DrissionPage 过滑动验证码

首先安装:DrissionPage

pip install DrissionPage

安装ddddocr:

pip install ddddocr

代码示例:

from DrissionPage import ChromiumPage,ChromiumOptions
import random
import time
import ddddocr

# 浏览器路径
path = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
co = ChromiumOptions().set_browser_path(path)

# 打开浏览器(全局)
page = ChromiumPage(co)
# 目标网站
page.get("https://cszg.mca.gov.cn/biz/ma/csmh/filter/slideCaptchaindex.html",retry=3,interval=2,timeout=15)

# 滑块的运动轨迹
def get_tracks(distance):
    value = round(random.uniform(0.55,0.78),2)
    v,t,sum1 = 0,0.3,0
    plus = []
    mid = distance * value
    while sum1 < distance:
        if sum1 < mid:
            a = round(random.uniform(2.5,3.5),1)
        else:
            a = -round(random.uniform(2.0,3.0),1)
        s = v * t + 0.5 * a * (t ** 2)
        v = v + a * t
        sum1 += s
        plus.append(round(s))
    return plus


for i in range(5):
    # 获取滑块背景图
    background_bytes = page.ele('xpath://*[@id="oriImg"]').src()
    # 获取缺口图片
    cut_bytes = page.ele('xpath://*[@id="cutImg"]').src()
    # 识别滑块缺口并获得滑动轨迹
    det = ddddocr.DdddOcr(det=False,ocr=False,show_ad=False)
    # 获得滑块距离(通过滑块图片和缺口图片计算出滑块的距离)
    result = det.slide_match(cut_bytes,background_bytes,simple_target=True)
    print("滑块距离:",result)
    # 获得滑块运动轨迹
    offset = result['target'][0]
    tracks = get_tracks(offset)
    print("滑动轨迹:",tracks)
    # 滑动滑块
    # 按动鼠标左键不放
    page.actions.hold('xpath://*[@id="slider"]')
    # 鼠标滑动
    for track in tracks:
        # 使鼠标相对当前位置移动若干距离
        page.actions.move(offset_x=track,offset_y=round(random.uniform(1.0,3.0),1),duration=.1)
    time.sleep(0.1)
    # 释放鼠标左键
    page.actions.release('xpath://*[@id="slider"]')
    # 截图
    page.ele('xpath://*[@id="captchadiv"]').get_screenshot(path='./captcha.jpg')
    time.sleep(5)
    # 如果验证失败
    if '验证' in page.ele('xpath:/html/body/div[6]/div/div[2]').text:
        print('滑动失败,刷新滑块')
        page.ele('xpath://*[@id="captchadiv"]/div[1]/img').click()
    else:
        print("滑动成功")
        break

print('开始采集')

 

posted @ 2024-12-04 14:32  离岸少年  阅读(67)  评论(0编辑  收藏  举报