繁体   English   中英

Python 请求响应 403 禁止

[英]Python requests response 403 forbidden

所以我试图抓取这个网站: https://www.auto24.ee我能够毫无问题地从中抓取数据,但今天它给了我“响应 403”。 我尝试使用代理,将更多信息传递给标头,但不幸的是似乎没有任何效果。 我在 inte.net 上找不到任何解决方案,我尝试了不同的方法。 之前工作的代码没有任何问题:

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)

您需要找到User-Agent 因此,打开浏览器并从developer tools中找到GET requestUser-Agent header 或按Ctrl+Shift+I

在此处输入图像描述

这是您如何找到不同浏览器的User-Agent的方法。

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 的任意方法,但该网站正在按预期工作。 必须有大量通过标头和 cookies 提交的数据表明您的请求有效,并且由于您只是提交了一个用户代理,因此触发了 CloudFlare。 简单地欺骗另一个用户代理甚至不足以触发验证码,CloudFlare 会检查很多东西。

我建议您在此处查看 selenium,因为它模拟了真实的浏览器,或者研究指南(可能?)通过请求绕过 Cloudflare。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM