资讯 小学 初中 高中 语言 会计职称 学历提升 法考 计算机考试 医护考试 建工考试 教育百科
栏目分类:
子分类:
返回
空麓网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
空麓网 > 计算机考试 > 软件开发 > 后端开发 > Python

Python爬虫识别验证码自动化登入哔哩哔哩Web端

Python 更新时间: 发布时间: 计算机考试归档 最新发布

Python爬虫识别验证码自动化登入哔哩哔哩Web端

项目:

通过Python爬虫自动化登入哔哩哔哩网页端账号


Python外部库

需要提前安装的库

import timefrom selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as Ecfrom selenium.webdriver.common.by import Byfrom chaojiying import Chaojiying_Clientfrom selenium.webdriver.common.action_chains import ActionChains

提示
1,需要在python包中的Scripts文件夹中下载与Google Chrome浏览器版本相同的浏览器驱动

2,使用了超级鹰的验证码技术需要提前注册超级鹰账号
超级鹰官方网站:http://www.chaojiying.com


项目代码

import timefrom selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as Ecfrom selenium.webdriver.common.by import Byfrom chaojiying import Chaojiying_Clientfrom selenium.webdriver.common.action_chains import ActionChainsclass bilibili:    def __init__(self):        # 构造方法或者初始化方法        self.driver = webdriver.Chrome()        self.url = 'https://www.bilibili.com/'        self.driver.maximize_window()    def Verification_code_image(self):        self.driver.get(self.url)        # 单击导航栏的登图标 弹出登录模块        WebDriverWait(self.driver, 10).until(Ec.presence_of_element_located((By.CLASS_NAME, 'header-login-entry')))        self.driver.find_element_by_class_name('header-login-entry').click()                """哔哩哔哩的 账号 密码"""        user_name = '写入账号'        user_pwd = '写入密码'                # 定位账号块并输入账号        WebDriverWait(self.driver, 20).until(            Ec.presence_of_element_located((By.XPATH, '/html/body/div[3]/div/div[4]/div[2]/form/div[1]/input')))        self.driver.find_element_by_xpath('/html/body/div[3]/div/div[4]/div[2]/form/div[1]/input').send_keys(user_name)        # 定位密码块并输入密码        WebDriverWait(self.driver, 10).until(            Ec.presence_of_element_located((By.XPATH, '/html/body/div[3]/div/div[4]/div[2]/form/div[3]/input')))        self.driver.find_element_by_xpath('/html/body/div[3]/div/div[4]/div[2]/form/div[3]/input').send_keys(user_pwd)        # 单击登录键    :要等待5秒在单击登录键,不然验证码图片加载不出来        time.sleep(5)        self.driver.find_element_by_xpath('/html/body/div[3]/div/div[4]/div[2]/div[2]/div[2]').click()        # 抠图 等待    :截取验证码图片        WebDriverWait(self.driver, 10).until(            Ec.presence_of_element_located((By.CLASS_NAME, 'geetest_holder.geetest_silver')))        self.driver.find_element_by_class_name('geetest_holder.geetest_silver').screenshot('哔哩.png')    def dock_chaojiying(self):        # 使用超级鹰识别点选验证码图片        chaojiying = Chaojiying_Client('超级鹰账号', '超级鹰密码', '软件ID')        im = open('哔哩.png', 'rb').read()        result = chaojiying.PostPic(im, '9004')    #9004是验证码类型编号                # 坐标多选,返回1~4个坐标,如:x1,y1|x2,y2|x3,y3>>[[x1,y1],[x2,y2],[x3,y3]]        coordinates = result.get('pic_str').split('|')        locations = [[int(number) for number in coordinate.split(',')] for coordinate in coordinates]        # print(locations)        # 以列表形式返回 坐标        return locations    def simulated_click(self):        self.Verification_code_image()        # 定位验证码图片 并使用超级鹰返回的坐标 按顺序单击正确的验证码        img = WebDriverWait(self.driver, 10).until(            Ec.presence_of_element_located((By.CLASS_NAME, 'geetest_holder.geetest_silver')))        for location in self.dock_chaojiying():            ActionChains(self.driver).move_to_element_with_offset(img, location[0], location[1]).click().perform()            time.sleep(2)        # 点击确定按钮        self.driver.find_element_by_class_name('geetest_commit_tip').click()        time.sleep(2)        print("登入成功")if __name__ == '__main__':    bi = bilibili()  # 实例化bilibili类    bi.simulated_click()

转载请注明:文章转载自 http://www.konglu.com/
本文地址:http://www.konglu.com/it/1098373.html
免责声明:

我们致力于保护作者版权,注重分享,被刊用文章【Python爬虫识别验证码自动化登入哔哩哔哩Web端】因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理,本文部分文字与图片资源来自于网络,转载此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请立即通知我们,情况属实,我们会第一时间予以删除,并同时向您表示歉意,谢谢!

我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2023 成都空麓科技有限公司

ICP备案号:蜀ICP备2023000828号-2