【无标题】

使用python通过selenium、seleniumwire 以及第三方验证码平台实现百度滑块验证码验证
seleniumwire基于selenium之上实现可以抓取网路请求的请求体信息以及响应体信息

import json
from seleniumwire import webdriver
from selenium.webdriver.chrome.service import Service
# 实现规避检测
from selenium.webdriver import ChromeOptions
# 动作链
from selenium.webdriver import ActionChains
# 页面发生变化延迟后获取数据
from time import sleep
# 第三方验证码解析
from yunma.yunma import YdmVerify
# 生成随机字符
import random

def verification(frequency):
    cs = frequency
    # 实现规避检测
    option = ChromeOptions()
    option.add_argument('--incognito')
    option.add_experimental_option('excludeSwitches', ['enable-automation'])

    s = Service('../chromedriver')
    dov = webdriver.Chrome(service=s, options=option)

    while cs:
        # 请求地址
        dov.get(
            'https://wappass.baidu.com/***')
		
        # 提取输入框输入内容
        input_div = dov.find_element('xpath', '/html/body/div/div[1]/div/div/div/div/div[2]/section/div/div/div/input')
        text = int(random.random() * 10000000000)
        input_div.send_keys(text)
        # 点击按钮获取验证码
        btn = dov.find_element('xpath', '/html/body/div/div[1]/div/div/div/div/div[4]/input')
        btn.click()
        sleep(5)

        # 验证码图片url
        img = dov.find_elements('xpath', '/html/body/div[3]/div[1]/div/div[1]/img')[0]
        img_url = img.get_attribute("src")
        # 验证码图片
        img_name = './imgs/' + img_url.split("%")[0].split('=')[1] + '.jpeg'
        logger.info(f"此次验证码图片:{img_name} url地址:{img_url}")
        # 截取验证码图片
        img.screenshot(img_name)

        # 使用第三方解析
        Y = YdmVerify()
        with open(img_name, 'rb') as f:
            s = f.read()
        px = Y.rotate(image=s)
        # 滑块需要移动多少像素
        # 验证码滑块一像素等于旋转1.68224299度
        slide = px / 1.68224299

        hk_btn = dov.find_element('xpath', '/html/body/div[3]/div/div/div[2]/div[2]')

        # 点击长按指定的标签
        ActionChains(dov).click_and_hold(hk_btn).perform()

        print(slide)
        while slide > 0:
            if slide < 10:
                ActionChains(dov).move_by_offset(slide, 0).perform()
                break
            ActionChains(dov).move_by_offset(10, 0).perform()
            slide -= 10

        # 释放动作链
        ActionChains(dov).release().perform()
        sleep(1)
        list_requests = dov.requests
        requests_list = []
        for i in list_requests:
            if i.url == 'https://wappass.baidu.com/passport/getpass':
                body = str(i.response.body, 'utf-8')
                body = json.loads(body)
                requests_list.append(body)
                print("BBB", body['errInfo']['no'])
        if requests_list[1] == 400002:
            print("验证成功")
        cs -= 1
    dov.quit()


if __name__ == '__main__':
    # 设置执行次数
    verification(3)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值