我正在尝试使用selenium构建一个项目,我需要进入一个启用Cloudflare的网站。我想自己手动绕过它。但是,cloudflare页面继续重新加载Ray ID,不让我验证。我该怎么办?
这是代码片段:
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
如有任何建议,将不胜感激。
发布于 2021-09-13 14:53:26
我的一位朋友建议使用普通浏览器(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 in终端,以便代码继续运行。
我知道这不是百分之百实用,但是对于小的工作,我认为它是一个完美的。
饼干:
cookies = browser_cookie3.firefox(domain_name='WEBSITE-DOMAIN')
如果您更喜欢使用铬,请将firefox
更改为chrome
希望这个答案也适用于你!
https://stackoverflow.com/questions/69151714
复制相似问题