You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pachong/bili反反爬.py

143 lines
4.7 KiB

import time
from io import BytesIO
from PIL import Image
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from chaojiying import Chaojiying_Client
#账号密码自行设定
EMAIL = '18934861725' #b站账号
PASSWORD = '1258490786' #b站密码
# CHAOJIYING_USERNAME = '123456rom'
# CHAOJIYING_PASSWORD = '1234567'
CHAOJIYING_USERNAME = '123456yiyiyi' #超级鹰账号
CHAOJIYING_PASSWORD = '123456789' #超级鹰密码
# CHAOJIYING_SOFT_ID = 932562
CHAOJIYING_SOFT_ID = 932746 #用户中心>>软件ID 生成一个替换 96001
CHAOJIYING_KIND = 9004 #验证码类型
class Crackclick():
def __init__(self):
self.url = 'https://passport.bilibili.com/login'
self.browser = webdriver.Chrome() #谷歌浏览器
self.wait = WebDriverWait(self.browser, 20)
self.email = EMAIL
self.password = PASSWORD
self.chaojiying = Chaojiying_Client(CHAOJIYING_USERNAME, CHAOJIYING_PASSWORD, CHAOJIYING_SOFT_ID)
def open(self):
'''
打开网页,切换验证模式
'''
self.browser.get(self.url)
account = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'type-tab')))
account.click()
email = self.wait.until(EC.presence_of_element_located((By.ID, 'login-username')))
password = self.wait.until(EC.presence_of_element_located((By.ID, 'login-passwd')))
email.send_keys(self.email)
password.send_keys(self.password)
def refresh_code(self):
'''
刷新验证码
'''
button = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'geetest_refresh')))
return button
#获取验证码图片
def get_element(self):
'''获取验证图片对象'''
element = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'geetest_item_img')))
return element
def get_position(self):
'''获取验证码位置'''
element = self.get_element()
time.sleep(2)
elementt = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'geetest_item_img')))
location = elementt.location
size = elementt.size
top, bottom, left, right = location['y'], location['y']+size['height'], location['x'], location['x']+size['width']
return (top,bottom,left,right)
def get_screenshot(self):
'''获取网页截图'''
screenshot = self.browser.get_screenshot_as_png()#获取页面截图
screenshot = Image.open(BytesIO(screenshot))
return screenshot
def get_image(self,name='captcha.png'):
'''获取验证码图片'''
top, bottom, left, right = self.get_position()
print('验证码位置',top,bottom,left,right)
screenshot = self.get_screenshot()
captcha = screenshot.crop((left,top,right,bottom))
return captcha
#解析识别信息
def get_points(self, captcha_result):
'''
解析识别信息
'''
groups = captcha_result.get('pic_str').split('|')
locations = [[int(number) for number in group.split(',')] for group in groups]
return locations
#点击验证图片
def touch_click_words(self,locations):
'''
点击验证码
'''
for location in locations:
print(location)
ActionChains(self.browser).move_to_element_with_offset(self.get_element(),location[0],location[1]).click().perform()
time.sleep(1)
#点击登录
def login(self):
'''
点击登录按钮
'''
button = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'btn.btn-login')))
button.click()
time.sleep(5)
#点击确认
def confirm(self):
'''
点击确认按钮
'''
button = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'geetest_commit_tip')))
button.click()
#控制流程
def crack(self):
'''入口'''
self.open()
self.login()
# button = self.refresh_code()
# button.click()
image = self.get_image()
bytes_array = BytesIO()
image.save(bytes_array, format='PNG')
# 识别验证码
result = self.chaojiying.PostPic(bytes_array.getvalue(), CHAOJIYING_KIND)
print(result)
locations = self.get_points(result)
self.touch_click_words(locations)
self.confirm()
if __name__ == "__main__":
crack = Crackclick()
crack.crack()