cmis123456789 2024-04-20 18:24 采纳率: 16.7%
浏览 33

爬虫程序人机校验后不跳转

爬虫,用selenium打开chrome浏览器,提示人机验证、需要在框内打勾。手动打钩(实力有限,不知道该怎么在代码实现打钩)后,页面不跳转。

  1. 人机验证提示图片

    img

  2. 扒出来的人机验证的代码
    python
         <div class="main-wrapper" role="main"><div class="main-content">
             <h1 class="zone-name-title h1">novel.munpia.com</h1>
             <h2 id="challenge-running" class="h2">请完成以下操作,验证您是真人。</h2>
             <div id="challenge-stage" style="display: flex;">
                 <div id="turnstile-wrapper" class="spacer">
                     <div>
                         <iframe src="https://challenges.cloudflare.com/cdn-cgi/challenge-platform/h/b/turnstile/if/ov2/av0/rcv0/0/db90n/0x4AAAAAAADnPIDROrmt1Wwj/light/normal" 
                         allow="cross-origin-isolated; fullscreen" sandbox="allow-same-origin allow-scripts allow-popups" id="cf-chl-widget-db90n" tabindex="0" 
                         title="包含  Cloudflare  安全质询的小组件 " style="border: none; overflow: hidden; width: 300px; height: 65px;">
                         </iframe>
                         <input type="hidden" name="cf-turnstile-response" id="cf-chl-widget-db90n_response">
                     </div>
                 </div>
             </div>
    
  1. 我的代码
    options = webdriver.ChromeOptions()
    #options.add_argument('--headless')
    # 设置参数
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    chrome = webdriver.Chrome(options=options)
    chrome.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
        "source": """
            # 这里的操作大概就是把控制台中的window.navigator.webdriver =undefined  赋值   因为人机操作会认为是Ture
            Object.defineProperty(navigator, 'webdriver', {
              get: () => undefined
            })
          """
    })

    chrome.get(source_url)
    time.sleep(10)
    print(chrome.page_source)
    chrome.get(source_url)

  • 写回答

6条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-04-20 18:27
    关注

    引自免费微信小程序:皆我百晓生

    看起来你在尝试通过Selenium进行网页自动化,遇到了Cloudflare的ReCaptcha挑战。Cloudflare ReCaptcha是为了防止机器人自动访问而设计的一种人机识别机制。要解决这个问题,你需要一个能够处理图形验证码的服务,比如使用第三方库pytesseract配合OpenCV来识别图像,或者使用专门处理验证码的API服务。由于这里无法提供实时的图像识别服务,我可以给你一个基本的代码框架,你可以根据这个框架自行添加相应的功能。

    首先确保安装了必要的库:

    pip install selenium Pillow pytesseract requests
    

    然后你可以参考以下代码片段,这部分代码没有包括验证码识别的部分,因为这通常需要额外的服务或者库支持。你可以自己搜索如何使用pytesseract识别图像,或者购买可以处理这类验证码的API服务。

    import time
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    from PIL import ImageGrab
    import pytesseract
    import cv2
    import numpy as np
    
    def handle_recaptcha(image_path):
        # 在这里添加你的验证码识别逻辑,例如使用pytesseract识别
        # 假设已经有一个函数get_solution可以从image_path获取到验证码答案
        solution = get_solution(image_path)
        return solution
    
    options = webdriver.ChromeOptions()
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    # 添加以下行以允许截取屏幕截图
    options.add_argument('--disable-infobars')
    options.add_argument('--start-maximized')
    
    driver = webdriver.Chrome(options=options)
    
    source_url = 'http://www.example.com'  # 替换为实际网址
    
    driver.get(source_url)
    time.sleep(5)  # 等待页面加载
    
    # 检查是否存在验证码
    while True:
        try:
            recaptcha_element = driver.find_element_by_tag_name('iframe')
            break
        except Exception:
            time.sleep(1)  # 如果未找到,等待并重试
    
    # 截取验证码图像
    driver.save_screenshot('captcha.png')
    img = ImageGrab.grabclipboard()
    
    # 使用pytesseract或其他方式识别验证码,此处仅为示例
    solution = handle_recaptcha('captcha.png')  # 获取验证码答案
    
    # 将验证码输入到相应位置
    driver.switch_to.frame(recaptcha_element)
    captcha_field = driver.find_element_by_id('cf-chl-widget-db90n_response')
    captcha_field.send_keys(solution)
    time.sleep(1)  # 确保验证码已输入
    
    # 提交表单或者触发下一步操作(根据实际情况调整)
    driver.find_element_by_xpath('//button[text()="提交"]').click()
    
    # 关闭浏览器
    driver.quit()
    

    在这个例子中,我提供了基本的代码结构,但是并没有实现验证码识别部分。你需要自己编写或引入一个能够识别验证码的函数get_solution,它应该接收图像文件路径作为输入,并返回识别出的验证码字符串。对于复杂的验证码,可能需要更高级的图像处理技术或者商业服务来准确识别。如果你不打算手动输入验证码,那么这部分代码是必须的。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 4月20日