百度旋转验证码破解

最近有很多网友反馈他那里的百度验证码是有随机阴影的,所以花了几周时间专门进行了针对性研究,请查看《百度随机阴影旋转验证码破解》

识别结果展示

识别代码

下面使用 python 调用 selenium 可视化查看破解效果(达到上面动图效果)。如果有问题可以留言,也可以CSDN私信我。

__author__ = "dengxinyan"

import os
import sys
sys.path.append(os.path.abspath(os.path.dirname(os.path.abspath(os.path.dirname(__file__)))))
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver import ActionChains
from Common3.Common3 import *

def get_img(url):
    header = {
        "Host": "passport.baidu.com",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0",
        "Accept": "image/avif,image/webp,*/*",
        "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
        "Accept-Encoding": "gzip, deflate, br",
        "Referer": "https://wappass.baidu.com/",
        "Connection": "keep-alive",
        "Cookie": "BAIDUID=7999A864ECB04BF96DC42141762CF08C:FG=1; BIDUPSID=310DBCE84D6A8388D88171F49E098D2D; PSTM=1634536706; BDRCVFR[gltLrB7qNCt]=mk3SLVN4HKm; delPer=0; PSINO=7; H_PS_PSSID=34837_34439_34067_31254_34741_34525_34584_34504_34706_34806_34578_26350_34725_22158_34691_34671; BDORZ=FFFB88E999055A3F8A630C64834BD6D0",
        "Sec-Fetch-Dest": "image",
        "Sec-Fetch-Mode": "no-cors",
        "Sec-Fetch-Site": "same-site",
        "Pragma": "no-cache",
        "Cache-Control": "no-cache",
    }
    response = requests.get(url=url,headers=header)

    if response.status_code == 200:
        img = Image.open(BytesIO(response.content))

    return PIL_base64(img)

# 识别
def shibie(base64_img):
    url = "http://www.detayun.cn/tool/verify_code_identify/"
    header = {
        "Host": "www.detayun.cn",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:101.0) Gecko/20100101 Firefox/101.0",
        "Accept": "application/json, text/javascript, */*; q=0.01",
        "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
        "Referer": "http://www.detayun.cn/tool/verifyCodeIdentifyPage/?verify_idf_id=9",
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        "X-Requested-With": "XMLHttpRequest",
        "Content-Length": "134652",
        "Origin": "http://www.detayun.cn",
        "Connection": "keep-alive",
        "Cookie": 'Hm_lvt_5ab812d883080ed21d18ea285c143720=1653617893,1653663725,1653870987,1653957541; Hm_lvt_511c569d5eaf2a35b42cff1481cd2dc9=1653352537,1653440016,1653960212; Hm_lvt_3eecc7feff77952670b7c24e952e8773=1653959999,1654048300,1654131249; Hm_lpvt_3eecc7feff77952670b7c24e952e8773=1654139189; token="MTY1NDE0Mjc4Ny4yOTc5Mzc2OjE0YWZmMjM5ZjE3ZjI4OTMzZjFiZmU3NTkzY2U3MGIyNDU4ZWZkODQ="; sessionid=pm06y1q06vgpmp5n5yb6bz4ogkqjqzab',
        "Pragma": "no-cache",
        "Cache-Control": "no-cache",
    }
    data = {
        'verify_idf_id':'9',
        'img_base64':base64_img,
        'words':'',
    }
    response = requests.post(url=url,headers=header,data=data)
    if response.json()['code'] == 401:
        print('请登录识别账号,更新Cookie。登录地址:http://www.detayun.cn/account/loginPage/')
    
    return int(str(response.json()['data']['res_str']).replace('顺时针旋转','').replace('度',''))


if __name__ == '__main__':
    # 可以加一些设置
    options = webdriver.ChromeOptions()
    driver = webdriver.Chrome(executable_path='.\webdriver\chromedriver.exe', options=options)

    # 访问百度验证码页面
    driver.get('https://wappass.baidu.com/static/captcha/tuxing.html?ak=33c48884b7df83d4230e07cbcd0d07fd&backurl=https%3A%2F%2Faiqicha.baidu.com×tamp=1634288600&signature=02fafac36a570b03a3c9100ec68b02e5')

    # 等待滑块出现
    WebDriverWait(driver, 10).until(lambda x: x.find_element_by_xpath('//div[contains(@id,"vcode-spin-button")]'))
    yzm_button = driver.find_element_by_xpath('//div[contains(@id,"vcode-spin-button")]')
    time.sleep(5)
    move_x = 100

    # 等待验证码出现
    WebDriverWait(driver, 10).until(lambda x: x.find_element_by_xpath('//img[contains(@id,"vcode-spin-img")]'))
    img_src = driver.find_element_by_xpath('//img[contains(@id,"vcode-spin-img")]').get_attribute('src')
    # 下载图片并转化为base64
    img_base64 = get_img(img_src)
    # 识别图片旋转角度
    move_x = shibie(img_base64)
    # 通过旋转角度 * 滑动系数 = 滑动距离
    move_x = move_x * 0.58
    # 开始滑动
    action = ActionChains(driver)
    action.click_and_hold(yzm_button).perform()  # 鼠标左键按下不放
    action.move_by_offset(move_x, 0)
    action.release().perform()  # 释放鼠标

    time.sleep(1000)

你可能感兴趣的:(验证码识别,python,图像处理)