python selinium-wire破解滑动验证码获取请求头

本文介绍了一段Python脚本,通过Selenium和BrowserMobProxy实现网站搜索过程中的滑动验证码识别。作者展示了如何定位元素并模拟滑动操作,核心内容包括图像处理、坐标跟踪和webdriver操作。
摘要由CSDN通过智能技术生成
#!/usr/bin/env python
# encoding: utf-8
# @author: jack
# @contact: 935650354@qq.com
# @site: https://www.cnblogs.com/jackzz


import re
import time
import random
import requests
import selenium
from PIL import Image
from seleniumwire import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from io import BytesIO
from selenium.webdriver.common.action_chains import ActionChains
from browsermobproxy import Server




def main(driver, element, x):
    # 1为完整图、2为有缺口图
    # image1 = get_img(driver, 'oriImg', 1)
    # image2 = get_img(driver, 'cutImg', 2)
    #
    # x = get_diff_location(image1, image2)
    tracks = get_track(x)
    time.sleep(0.5)
    ActionChains(driver).click_and_hold(element).perform()
    # for x in tracks:
    ActionChains(driver).move_by_offset(xoffset=x, yoffset=0).perform()
    ActionChains(driver).release(element).perform()
    # time.sleep(0.5)


if __name__ == '__main__':

    driver = webdriver.Chrome(executable_path=r"C:\Users\Administrator\PycharmProjects\chinaSpider\chromedriver.exe")
    driver.maximize_window()
    driver.get('*********************************')

    inputLabel = driver.find_element_by_class_name("search_input");
    inputLabel.send_keys("协会")

    searchButton = driver.find_element_by_class_name("search_button")
    searchButton.click()
    antModel = driver.find_element_by_class_name("ant-modal")
    if antModel:
        # noinspection PyBroadException
        try:
            x = 150
            count = 100
            # waiting slidingVC loading
            wait = WebDriverWait(driver, 2)
            try:
                element = wait.until(EC.invisibility_of_element((By.CLASS_NAME, 'slider')))
            except Exception:
                choseBtn = driver.find_element_by_class_name('ant-modal-close')
                choseBtn.click()
                time.sleep(2)
                searchButton.click()
                element = wait.until(EC.invisibility_of_element((By.CLASS_NAME, 'slider')))
            while count > 0:
                switch = 0
                if x > 250:
                    x = 152
                try:
                    driver.find_element_by_class_name("slider_success")
                    switch = 1
                except selenium.common.exceptions.NoSuchElementException:
                    switch = 0

                if switch == 1:
                    refreshButton = driver.find_element_by_xpath('//*[@id="captchaContainer"]/div[1]/img')
                    refreshButton.click()
                    time.sleep(1)
                main(driver, element, x)
                try:
                    succes = wait.until(
                        EC.presence_of_element_located((By.XPATH, '//div[@class="search_title"]')))
                    if succes:
                        # Access requests via the `requests` attribute
                        print(driver.requests[len(driver.requests)-2].params)
                        # for request in driver.requests:
                        #     print(request.params)

                        print('恭喜你!识别成功...')
                        break
                except Exception as e:
                    print('识别错误,继续,' + "滑动距离为:%d" % x)
                    count -= 1
                    x += 8
        finally:
            driver.quit()
    else:
        print("NOCAPTCHE")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值