python小白自己查阅资料写的程序,若有缺陷和不足烦请各位大佬指正!加上了selenium反识别。采用的是模拟用户登录的方式(非cookie) ,验证码部分采用超级鹰识别接口,返回坐标,再使用动作链点击指定文字,通过机器人检测。

# Author:Yuan Jinmin
# -*- coding = utf-8 -*-
# @Time  :2022/2/16 14:47
# @Author:YJM
# @Site  :
# @File  :seleniumDemo.py
# @Software: IntelliJ IDEA
import time

from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver

# !!!需要自行导入超级鹰

if __name__ == '__main__':
    url = 'https://passport.bilibili.com/login'
    username = 'B站账号' # !!!要修改成自己的
    password = '密码'

    # 隐藏webdriver方法,不用,会出现手机验证码界面
    options = webdriver.ChromeOptions()
    options.add_experimental_option('excludeSwitches', ['enable-automation'])  # 设置为开发者模式
    options.add_argument("--disable-blink-features=AutomationControlled")

    driver = webdriver.Chrome(
        executable_path='!!!改成自己的谷歌驱动位置'
        , options=options)

    driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument",{
                               'source': '''Object.defineProperty(navigator, 'webdriver', { get: () =>undefined })'''})

    # # 最大化窗口
    # driver.maximize_window()
    driver.get(url=url)

    time.sleep(3)

    # 传入用户名
    driver.find_element_by_xpath('//*[@id="login-username"]').send_keys(username)
    time.sleep(0.5)
    # 传入密码
    driver.find_element_by_xpath('//*[@id="login-passwd"]').send_keys(password)
    time.sleep(0.5)

    # 点击登录按钮
    driver.find_element_by_xpath('//*[@id="geetest-wrap"]/div/div[5]/a[1]').click()

    # 截取验证码图片
    time.sleep(2)
    code_img_element = driver.find_element_by_xpath('/html/body/div[2]/div[2]')
    code_img = code_img_element.screenshot('code.png')  # 一气呵成
    # # 获取验证码图片框的左上角的坐标
    # location = code_img.location
    # print('code_img location:', location)
    # # 验证码的长和宽
    # size = code_img.size
    # print('code_img size:', size)

    # 交给超级鹰识别验证码,使用需要自行导包,官网有示例下载
    # 调用超级鹰平台接口识别验证码
    print('正在识别验证码...')
    chaojiying = chaojiying.Chaojiying_Client('超级鹰用户名', '密码',
                                              '软件ID')  
    # !!!要修改成自己的 用户名,密码,软件ID ,用户中心>>软件ID 生成一个替换 96001
    im = open('code.png', 'rb').read()  # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
    answer = chaojiying.PostPic(im, 9004)  # 9004  坐标多选,返回1~4个坐标,如:x1,y1|x2,y2|x3,y3  25
    # print(answer)  # 1902 验证码类型  官方网站>>价格体系 3.4+版 print 后要加()
    # 输出示例:
    # {'err_no': 0, 'err_str': 'OK', 'pic_id': '9168012087860700001', 'pic_str': '7261', 'md5': 'bf67edb09a6d203c34ac591c824d995b'}
    # 得到验证码
    code = answer['pic_str']

    # 坐标
    coordinate = [xy.split(sep=',') for xy in code.split(sep='|')]  # [['1', '2'], ['3', '4'], ['1', '2'], ['3', '4']]
    print('解析验证码成功!坐标为:', coordinate)

    # 遍历坐标列表,使用动作链点击列表中每个坐标,达到点击验证码的目的
    for xy in coordinate:
        x = xy[0]
        y = xy[1]
        ActionChains(driver).move_to_element_with_offset(code_img_element, x, y).click().perform()
        time.sleep(0.5)

    driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[6]/div/div/div[3]/a').click()

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐