cmis123456789 2024-04-22 12:54 采纳率: 16.7%
浏览 46

人机验证通过后跳转目标页面,无法获取到目标页面内容

selenium 驱动chrome抓取一个网站数据,有cloudflare的人机校验;浏览器打开后不关闭,人机校验通过后,页面跳转到了目标页面。但是chrome.pagesource获取不到目标页面内容,得到的还是人机校验界面的内容。

    options = webdriver.ChromeOptions()
    # 设置参数
    # 浏览器打开不关
    options.add_experimental_option("detach", True)
    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(5)  # 这里可能需要根据实际情况调整等待时间

    iframe_element = chrome.find_element(By.XPATH,"//iframe[@title='包含  Cloudflare  安全质询的小组件 ']")
    # 切换到iframe中
    chrome.switch_to.frame(iframe_element)
    click_element = chrome.find_element(By.CSS_SELECTOR, "input[type='checkbox']")
    actions = ActionChains(chrome)
    actions.move_to_element(click_element).perform()
    actions.click(click_element).perform()
    WebDriverWait(chrome, 20).until_not(EC.presence_of_element_located((By.XPATH, "//iframe[@title='包含 Cloudflare 安全质询的小组件 ']")))
    chrome.switch_to.default_content()
    WebDriverWait(chrome, 60).until(expected_conditions.url_contains('munpia.com'))
    print(chrome.page_source)


  • 写回答

7条回答 默认 最新

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

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

    在您的代码中,看起来您已经正确地处理了Cloudflare的反自动化检测,并且成功地通过了人机验证。然而,chrome.page_source 获取的是整个浏览器窗口的源代码,包括可能存在的多个iframe。因此,即使页面已经跳转,您仍然可能看到之前的人机验证界面。

    为了解决这个问题,您可以尝试在等待URL变化之后再次获取页面源码。这样可以确保您获取的是最终目标页面的源代码。以下是修改后的代码:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.action_chains import ActionChains
    
    # 创建Chrome选项
    options = webdriver.ChromeOptions()
    # 设置参数
    # 浏览器打开不关
    options.add_experimental_option("detach", True)
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    
    # 添加用户代理以模仿真实用户行为(可选)
    options.add_argument('--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3')
    
    # 初始化Chrome驱动
    chrome = webdriver.Chrome(options=options)
    
    # 执行脚本绕过Cloudflare的检测
    chrome.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
        "source": """
            # 这里的操作大概就是把控制台中的window.navigator.webdriver =undefined  赋值   因为人机操作会认为是Ture
            Object.defineProperty(navigator, 'webdriver', {
              get: () => undefined
            })
          """
    })
    
    # 访问源网址
    chrome.get("https://www.munpia.com/")
    
    # 等待页面加载完成
    WebDriverWait(chrome, 10).until(EC.presence_of_element_located((By.TAG_NAME, "body")))
    
    # 处理Cloudflare验证
    iframe_element = chrome.find_element(By.XPATH, "//iframe[@title='包含  Cloudflare  安全质询的小组件 ']")
    # 切换到iframe中
    chrome.switch_to.frame(iframe_element)
    click_element = chrome.find_element(By.CSS_SELECTOR, "input[type='checkbox']")
    actions = ActionChains(chrome)
    actions.move_to_element(click_element).perform()
    actions.click(click_element).perform()
    WebDriverWait(chrome, 20).until_not(EC.presence_of_element_located((By.XPATH, "//iframe[@title='包含 Cloudflare 安全质询的小组件 ']")))
    chrome.switch_to.default_content()
    
    # 等待目标页面加载
    WebDriverWait(chrome, 60).until(expected_conditions.url_contains('munpia.com'))
    
    # 在URL变化后再获取页面源码
    target_page_source = chrome.page_source
    
    print(target_page_source)
    
    # 关闭浏览器
    chrome.quit()
    

    这个版本的代码增加了对整个页面加载的等待,然后在URL变化后再次获取页面源码。这样应该能确保您获取到的是目标页面的源代码。如果还有问题,请检查网络请求是否正常,或者确认是否有其他安全措施阻止了爬虫访问。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 4月22日