【问题标题】:Python requests response 403 forbiddenPython请求响应403被禁止
【发布时间】:2022-01-18 23:58:57
【问题描述】:

所以我正在尝试抓取这个网站: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)

【问题讨论】:

  • 我认为你不需要欺骗用户代理。只要确保您避免使用该站点上/robots.txt 指定的资源

标签: python python-requests http-status-code-403 python-requests-html


【解决方案1】:

这里的代码

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 会检查很多东西。

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

更新 找到了 2 个 python 库 cloudcraper 和 cfscrape。除非您为高级版本付费,否则两者都不适用于此站点,因为它使用的是 cloudflare v2。

【讨论】:

  • 感谢您的回复,我自己并没有意识到这一点。至少现在我知道原因了。不幸的是,要为此开发一个验证码求解器并不容易。
  • 云耀斑的存在是可悲的!我确信有非常困难的方法可以克服它。我正在查看一些 cookie,发现有一些 cookie 与当前时间和日期相关联,这些 cookie 可能被操纵以绕过它。除此之外,这超出了我的范围。如果您有机会,请接受我的回答,以便其他人也可以解决此问题。祝你有美好的一天!
【解决方案2】:

你需要找到User-Agent。因此,打开浏览器并从developer tools 中找到GET requestUser-Agent 标头或按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)

另外,请尝试使用requests.Session()

import requests
session = requests.Session()
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 = session.get("https://www.auto24.ee/", headers=headers)
print(page.content.decode())

会话可能会解决问题或安装cfscrape。你可以在这里找到答案Python - Request being blocked by Cloudflare

更新

尝试pip install cloudscraper -U 了解更多信息,请参阅A Python module to bypass Cloudflare's anti-bot page。在issues中,你会发现a fix for Cloudflare v2

【讨论】:

  • 正如我所说,传递用户代理没有帮助。
  • 我已经更新了。立即尝试。
  • 正如@Keegan M所说,这是由CloudFlare保护引起的,因此解决起来并不容易。
  • 会话可能解决问题或安装cfscrapePython Request being blocked by Cloudflare
  • 单独一个会话没有任何区别。 cfscape 和 cloudcraper 都不可用,因为这是 cloudflare v2
猜你喜欢
  • 2016-08-17
  • 2016-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-28
  • 1970-01-01
  • 2014-10-31
相关资源
最近更新 更多