python破解b站验证码实现登陆
Python3爬虫实战【点触验证码】 — 模拟登陆bilibili
爬虫思路如下:
利用自动化爬虫工具 Selenium 模拟点击输入等操作来进行登录
分析页面,获取点触验证码的点触图片,通过将图片发送给超级鹰打码平台识别后获取坐标信息
根据超级鹰返回的数据,模拟坐标的点选,即可实现登录
一.准备工作
在开始之前,需要先注册一个超级鹰账号并申请一个软件ID,注册页面链接为:https://www.chaojiying.com/user/reg/,注册完成后需要在后台添加一个软件ID,进行充值获得积分,一般充一块钱就可以了。
二.爬虫构建
1.首先我可以到官方网站下载对应的 Python API,链接为:https://www.chaojiying.com/api-14.html ,
import random import time from io import BytesIO import requests 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 b站登陆.chaojiying import Chaojiying_Client #超级鹰api USERNAME = 'b站账号' PASSWORD = 'b站密码' CHAOJIYING_USERNAME = '超级鹰账号' CHAOJIYING_PASSWORD = '超级鹰密码' CHAOJIYING_SOFT_ID = 软件id CHAOJIYING_KIND = 9005 #验证码类型 class CrackTouClick(): def __init__(self): self.url = 'https://passport.bilibili.com/login' self.browser = webdriver.Chrome() self.browser.maximize_window() self.wait = WebDriverWait(self.browser, 20) self.username = USERNAME self.password = PASSWORD def open(self): """ 打开网页输入用户名密码 :return: None """ self.browser.get(self.url) user = 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'))) user.send_keys(self.username) password.send_keys(self.password) login_btn = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'a.btn.btn-login'))) # 随机暂停几秒 time.sleep(random.random() * 3) # 点击登陆按钮 login_btn.click() def pick_code(self): time.sleep(3) 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_Client(CHAOJIYING_USERNAME, CHAOJIYING_PASSWORD, CHAOJIYING_SOFT_ID) result = cjy.PostPic(img_content, '9005') # 发送图片并获取结果 if result['err_no'] == 0: # 对结果进行分析 position = result['pic_str'].split('|') # position = ['110,234','145,247','25,185'] position = [[int(j) for j in i.split(',')] for i in position] # position = [[110,234],[145,247],[25,185]] 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') certern_btn.click() return cjy, result def crack(self): """ 破解入口 :return: None """ self.open() self.pick_code() if __name__ == '__main__': crack = CrackTouClick() crack.crack()
原文链接:https://blog.csdn.net/weixin_30994671/article/details/113643305
目前正在学习Python中,如果有什么不对的地方 希望广大朋友,指出错误指出,深表感谢