Python - 请求被 Cloudflare 阻止 [英] Python - Request being blocked by Cloudflare
问题描述
我正在尝试登录网站.当我查看 print(g.text) 时,我没有返回我期望的网页,而是一个 cloudflare 页面,上面写着在访问之前检查您的浏览器"
I am trying to log into a website. When I look at print(g.text) I am not getting back the web page I expect but instead a cloudflare page that says 'Checking your browser before accessing'
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)
为什么在我设置会话后会发生这种情况?
Why is this occurring when I have set the session?
推荐答案
这是因为该页面使用 Cloudflare 的反机器人页面(或 IUAM).
绕过此检查您自己很难解决,因为 Cloudflare 会定期更改他们的技术.目前,他们会检查客户端是否支持 JavaScript,这可能会被欺骗.
我建议使用 cfscrape
模块来绕过这个.
要安装它,请使用pip install cfscrape
.您还需要安装 Node.js.
您可以像这样将请求会话传递到 create_scraper()
中:
This is due to the fact that the page uses Cloudflare's anti-bot page (or IUAM).
Bypassing this check is quite difficult to solve on your own, since Cloudflare changes their techniques periodically. Currently, they check if the client supports JavaScript, which can be spoofed.
I would recommend using the cfscrape
module for bypassing this.
To install it, use pip install cfscrape
. You'll also need to install Node.js.
You can pass a requests session into create_scraper()
like so:
session = requests.Session()
session.headers = ...
scraper = cfscrape.create_scraper(sess=session)
这篇关于Python - 请求被 Cloudflare 阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!