使用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)