我正在尝试登录一个网站。当我查看 print(g.text) 时,我没有返回我期望的网页,而是一个 cloudflare 页面,上面写着“访问前检查您的浏览器”
import requests
import time
s = requests.Session()
s.get('https://www.off---white.com/en/GB/')
headers = {'Referer': 'https://www.off---white.com/en/GB/login'}
payload = {
'utf8':'✓',
'authenticity_token':'',
'spree_user[email]': 'EMAIL@gmail.com',
'spree_user[password]': 'PASSWORD',
'spree_user[remember_me]': '0',
'commit': 'Login'
}
r = s.post('https://www.off---white.com/en/GB/login', data=payload, headers=headers)
print(r.status_code)
g = s.get('https://www.off---white.com/en/GB/account')
print(g.status_code)
print(g.text)
为什么在我设置会话后会发生这种情况?
原文由 Pthomas 发布,翻译遵循 CC BY-SA 4.0 许可协议
这是因为该页面使用了 Cloudflare 的反机器人页面(或 IUAM)。
绕过此检查很难自行解决,因为 Cloudflare 会定期更改其技术。目前,他们会检查客户端是否支持可以被欺骗的 JavaScript。
我建议使用
cfscrape
模块来绕过这个。要安装它,请使用
pip install cfscrape
。您还需要安装 Node.js 。您可以将请求会话传递给
create_scraper()
如下所示: