【问题标题】:Selenium Webdriver stuck at Cloadflare DDos Protection. How to bypass manually?Selenium Webdriver 卡在 Cloudflare DDos 保护上。如何手动绕过?
【发布时间】:2021-09-13 22:53:26
【问题描述】:

我正在尝试使用 selenium webdriver 构建一个项目,我需要进入一个启用了 Cloudflare 的网站。我想自己手动绕过它。但是,cloudflare 页面不断重新加载 Ray ID 并且不让我验证。我该怎么办?

这是代码sn-p:

from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
import undetected_chromedriver as uc

options = Options()
options.add_argument("ignore-certificate-errors")
options.add_argument("--no-sandbox")
options.add_argument("--disable-blink-features=AutomationControlled")
browser = uc.Chrome(executable_path=ChromeDriverManager().install(), chrome_options=options)

browser.get(BASE_URL + '/our-selection/')
input('Continue?')

在访问网站之前检查您的浏览器。这个过程是 自动的。您的浏览器将重定向到您请求的内容 很快。

请允许最多 5 秒...

Cloudflare 的 DDoS 保护

任何建议都将受到高度赞赏。

【问题讨论】:

    标签: python selenium web-scraping


    【解决方案1】:

    我的一个朋友建议使用普通(chrome 或 firefox)浏览器,打开网站并手动完成 cloudflare 步骤,然后在我的 python 代码中加载该浏览器的 cookie。它起作用了,我根本不需要硒。

    这是一个示例代码:

    import requests
    import browser_cookie3
    
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Language': 'en-US,en;q=0.5',
        'Connection': 'keep-alive',
        'Upgrade-Insecure-Requests': '1',
        'Sec-Fetch-Dest': 'document',
        'Sec-Fetch-Mode': 'navigate',
        'Sec-Fetch-Site': 'same-origin',
        'Sec-Fetch-User': '?1',
        'TE': 'trailers',
    }
    
    def getResponse(url):
        while True:
            cookies = browser_cookie3.firefox(domain_name='WEBSITE-DOMAIN')
            response = requests.get(url, headers=headers, cookies=cookies)
            time.sleep(1)
            if response.status_code == 403:
                input(f'Cloudflare Detected: Please verify on firefox then press ENTER!')
            elif response.status_code == 200:
                print(f'Response 200 {url}')
                return response
            elif response.status_code == 404:
                return 404
    

    所以你使用getResponse(URL) 而不是requests.get(URL)。该功能将检测 Cloudflare 是否阻止您,如果是,它将要求您在浏览器中完成任务,然后在终端中按 Enter 以继续运行代码。

    我知道这不是 100% 实用的,但对于小型工作,我认为它是完美的。

    对于 cookie:

    cookies = browser_cookie3.firefox(domain_name='WEBSITE-DOMAIN')
    

    如果您更喜欢使用 chrome,请将 firefox 更改为 chrome

    希望这个答案也适合你!

    【讨论】:

      猜你喜欢
      • 2016-01-19
      • 2021-07-24
      • 2022-09-27
      • 1970-01-01
      • 1970-01-01
      • 2022-11-21
      • 1970-01-01
      • 2021-09-18
      • 2021-08-05
      相关资源
      最近更新 更多