Selenium 应用程序在 Heroku 上托管时重定向到 Cloudflare 页面

2023-12-25

我制作了一个不和谐的机器人,它使用 selenium 访问网站并获取信息,当我在本地运行代码时,我没有任何问题,但是当我部署到 Heroku 时,我得到的第一个 URL 将我重定向到该页面Attention Required! | Cloudflare.

我努力了:

  • Selenium webdriver:修改 navigator.webdriver 标志以防止 selenium 检测 https://stackoverflow.com/questions/53039551/selenium-webdriver-modifying-navigator-webdriver-flag-to-prevent-selenium-detec/53040904#53040904

还有许多其他具有我使用的相同设置的:

options = Options()
options.binary_location = os.environ.get("GOOGLE_CHROME_BIN")
options.add_experimental_option("excludeSwitches", ["enable-logging", "enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument("--headless")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")
self.driver = webdriver.Chrome(executable_path=os.environ.get("CHROMEDRIVER_PATH"), options=options)
self.driver.execute_cdp_cmd('Network.setUserAgentOverride', {
    "userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.53 Safari/537.36'})

但这不起作用,代码仅在本地运行

PS:本地我在Windows上

Source of the page I'm redirected to: https://gist.github.com/rafalou38/9ae95bd66e86d2171fc8a45cebd9720c https://gist.github.com/rafalou38/9ae95bd66e86d2171fc8a45cebd9720c page source


如果Selenium https://stackoverflow.com/questions/54459701/what-is-selenium-and-what-is-webdriver/54482491#54482491 driven Chrome驱动程序 https://stackoverflow.com/questions/48079120/what-is-the-difference-between-chromedriver-and-webdriver-in-selenium/48080871#48080871发起谷歌浏览器 /questions/tagged/google-chrome 浏览上下文正在重定向到该页面...

...这意味着云耀 https://www.cloudflare.com/程序正在阻止您的程序访问AUT(测试中的应用程序).


Analysis

背后可能有几个原因云耀阻止访问如下:

  • 云耀已识别出您的程序 asabot并且访问被拒绝。您可以在中找到详细的讨论网站可以检测到您何时将 selenium 与 chromedriver 一起使用吗? https://stackoverflow.com/questions/33225947/can-a-website-detect-when-you-are-using-selenium-with-chromedriver/62520191#62520191.

由于以下因素,访问可能被拒绝:

  • 云耀正在试图应对一种可能的情况字典攻击 https://en.wikipedia.org/wiki/Dictionary_attack.
  • 您的系统IP已被列入黑名单云耀 for mining https://torrentfreak.com/the-pirate-bay-website-runs-a-cryptocurrency-miner-170916/ 比特币 or 门罗币使用您的系统。

在这些情况下,您最终会被重定向到captcha https://stackoverflow.com/questions/55501524/how-does-recaptcha-3-know-im-using-selenium-chromedriver/55502835#55502835 page.


Solution

在这些情况下,一个潜在的解决方案是使用未检测到的 chromedriver https://pypi.org/project/undetected-chromedriver/初始化Chrome 浏览上下文.

未检测到的 chromedriver https://github.com/ultrafunkamsterdam/undetected-chromedriver是一个优化的 Selenium Chromedriver 补丁,不会触发 Distill Network / Imperva / DataDome / Botprotect.io 等反机器人服务。它会自动下载驱动程序二进制文件并对其进行修补。

  • 代码块:

    import undetected_chromedriver as uc
    from selenium import webdriver
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    driver = uc.Chrome(options=options)
    driver.get('https://bet365.com')
    

替代解决方案

另一种解决方案是通过以下方式将您的 IP 地址列入白名单蜜罐计划 https://www.projecthoneypot.org/search_ip.php网站,您可以在标题为的视频中找到详细的端到端流程注意 还需要一步验证码 CloudFlare 错误 https://www.youtube.com/watch?v=rGFKTkqEsu8.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Selenium 应用程序在 Heroku 上托管时重定向到 Cloudflare 页面 的相关文章

随机推荐