前言
本文代码借鉴了《python3网络爬虫开发实战》,个人改进主要为针对为判断b站登录是否成功的部分以及返回给超级鹰平台的错误图片代码(设置全局变量)。
"""
Time: ${DATE} ${TIME}
Author: Hikari
Version: V 0.1
File: ${NAME}.py
"""
from io import BytesIO
from PIL import Image
import time
import requests
from selenium.webdriver import ActionChains
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from chaojiying import Chaojiying
pic_id =''
EMAIL='your email'
PASSWORD = 'your passwd'
CHAOJIYING_USERNAME = 'your chaojiying username'
CHAOJIYING_PASSWORD = 'your chaojiying passwd'
CHAOJIYING_SOFT_ID = 924124
CHAOJIYING_KIND = 9004
class CrackTouClick():
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(CHAOJIYING_USERNAME, CHAOJIYING_PASSWORD, CHAOJIYING_SOFT_ID)
def open(self):
self.browser.get(self.url)
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 get_touclick_button(self):
button = self.wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.btn-box > a ')))
return button
def pick_code(self):
global pic_id
time.sleep(5)
ele = self.browser.find_elements_by_css_selector('img.geetest_item_img')
if (len(ele)==1):
pick_img_label = self.browser.find_element_by_css_selector('img.geetest_item_img')
src = pick_img_label.get_attribute('src')
img_content = requests.get(src).content
f = BytesIO()
f.write(img_content)
img0 = Image.open(f)
scale = [pick_img_label.size['width'] / img0.size[0],
pick_img_label.size['height'] / img0.size[1]]
cjy = Chaojiying(CHAOJIYING_USERNAME, CHAOJIYING_PASSWORD, CHAOJIYING_SOFT_ID)
result = cjy.post_pic(img_content, '9005')
if result['err_no'] == 0:
position = result['pic_str'].split('|')
position = [[int(j) for j in i.split(',')] for i in position]
for items in position:
ActionChains(self.browser).move_to_element_with_offset(pick_img_label, items[0] * scale[0], items[1] * scale[1]).click().perform()
time.sleep(1)
time.sleep(2)
certern_btn = self.browser.find_element_by_css_selector('div.geetest_commit_tip')
time.sleep(1)
certern_btn.click()
def detect(self):
current=self.browser.current_url
if (current == 'https://passport.bilibili.com/account/security#/home'or current =='https://www.bilibili.com/'):
print('登陆成功')
else:
self.chaojiying.report_error(pic_id)
self.pick_code()
self.detect()
def crack(self):
self.open()
time.sleep(2)
button = self.get_touclick_button()
button.click()
self.pick_code()
time.sleep(3)
self.detect()
if __name__ == '__main__':
crack = CrackTouClick()
crack.crack()
|