所以我想抓取这个网站:https://www.auto24.ee我能够毫无问题地从中抓取数据,但今天它给了我“响应 403”。我尝试使用代理,将更多信息传递给标头,但不幸的是似乎没有任何效果。我在互联网上找不到任何解决方案,我尝试了不同的方法。
之前运行没有任何问题的代码:
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36',
}
page = requests.get("https://www.auto24.ee/", headers=headers)
print(page)
代码在这里
import requests
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36'}
page = requests.get("https://www.auto24.ee/", headers=headers)
print(page.text)
总是会得到如下的东西
<div class="cf-section cf-wrapper">
<div class="cf-columns two">
<div class="cf-column">
<h2 data-translate="why_captcha_headline">Why do I have to complete a CAPTCHA?</h2>
<p data-translate="why_captcha_detail">Completing the CAPTCHA proves you are a human and gives you temporary access to the web property.</p>
</div>
<div class="cf-column">
<h2 data-translate="resolve_captcha_headline">What can I do to prevent this in the future?</h2>
<p data-translate="resolve_captcha_antivirus">If you are on a personal connection, like at home, you can
run an anti-virus scan on your device to make sure it is not infected with malware.</p>
该网站受 CloudFlare 保护。通过标准方式,通过请求或 selenium 等自动化方式访问网站的可能性很小。您看到 403 是因为您的客户端被检测为机器人。可能有一些绕过 CloudFlare 的任意方法可以在其他地方找到,但该网站正在按预期工作。必须通过标头和 cookie 提交大量数据来表明您的请求有效,并且由于您仅提交用户代理,因此会触发 CloudFlare。仅仅欺骗另一个用户代理还不足以不触发验证码,CloudFlare 会检查很多事情。
我建议你看看硒here因为它模拟真实的浏览器,或者研究指南(可能?)通过请求绕过 Cloudflare。
Update找到2个python库cloudscraper和cfscrape。除非您付费购买高级版本,否则这两者都不适用于该网站,因为它使用 cloudflare v2。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)