【问题标题】:How to bypass Cloudflare bot protection in selenium如何绕过 Selenium 中的 Cloudflare bot 保护
【发布时间】:2021-07-24 05:41:12
【问题描述】:

出于教育目的,我需要从站点获取一些信息,但是由于保护,我无法发送请求。我得到了典型的 Checking-your-browser 页面首先出现,然后我被反复重定向。 我如何在 python selenium 中绕过这种保护?

【问题讨论】:

    标签: python selenium selenium-chromedriver cloudflare


    【解决方案1】:

    我很久以前就遇到过这个问题,我能够解决它。使用下面的代码并享受:)

    options = webdriver.ChromeOptions()
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    options.add_argument("--disable-blink-features=AutomationControlled")
    driver = webdriver.Chrome(options=options, executable_path=r"webdriver\chromedriver.exe")
    

    ///////// 编辑////////////// 这种方式现在行不通了!

    【讨论】:

    • 通常,Cloudflare 验证大约需要 5 秒。您需要在获取 page_source 之前添加一个计时器。只需在 driver.page_source 之前添加 time.sleep(6)
    【解决方案2】:

    2021 年 7 月解决方案

    只需在 chrome 选项中添加用户代理参数并将用户代理设置为任何值

    ops = Options() ua='cat' ops.add_argument('--user-agent=%s' % ua) driver=uc.Chrome(executable_path=r"C:\chromedriver.exe",chrome_options=ops)

    【讨论】:

    • 也不适用于此。我正在使用 fake_useragent 中的随机用户代理,但无法正常工作。
    猜你喜欢
    • 2016-01-19
    • 1970-01-01
    • 2022-09-27
    • 2021-09-18
    • 2021-08-05
    • 2023-02-03
    • 2022-09-27
    • 2020-09-22
    • 2016-11-19
    相关资源
    最近更新 更多