尝试从中国网站获取响应时的状态代码 412

发布于 2025-01-18 07:49:09 字数 1082 浏览 3 评论 0原文

我的代码下面返回; wespons [412]>在Python上,但是从Chrome访问没有问题。

import requests

response = requests.get("http://dzs.customs.gov.cn/dzs/2747042/3995819/ls/3997385", # /index.html
                     headers={'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
                              'Accept-Encoding': 'gzip, deflate',
                              'Accept-Language': 'en-US,en;q=0.9,pt-BR;q=0.8,pt;q=0.7',
                              'Cache-Control': 'max-age=0',
                              'Connection': 'keep-alive',                                  
                              'Host': 'dzs.customs.gov.cn',
                              'Referer': 'http://dzs.customs.gov.cn/dzs/2747042/3995819/ls/',
                              'Upgrade-Insecure-Requests': '1',
                              'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36'})

print(response)

My code below returns <Response [412]> on Python, but accessing from Chrome has no problem.

import requests

response = requests.get("http://dzs.customs.gov.cn/dzs/2747042/3995819/ls/3997385", # /index.html
                     headers={'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
                              'Accept-Encoding': 'gzip, deflate',
                              'Accept-Language': 'en-US,en;q=0.9,pt-BR;q=0.8,pt;q=0.7',
                              'Cache-Control': 'max-age=0',
                              'Connection': 'keep-alive',                                  
                              'Host': 'dzs.customs.gov.cn',
                              'Referer': 'http://dzs.customs.gov.cn/dzs/2747042/3995819/ls/',
                              'Upgrade-Insecure-Requests': '1',
                              'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36'})

print(response)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南汐寒笙箫 2025-01-25 07:49:09

使用 selenium-wire,带有未发现的Chromedriver

硒线将与未检测到的胶合丝集成
它在您的环境中。该库将透明地修改
ChromeDriver防止其触发反机器人措施
网站。

如果您想利用此优势,请确保
安装了Undetected_chromedriver:

  pip安装未检测到的 - 染色器
 

然后在您的代码中,导入seleniumwire.undetected_chromedriver
软件包:

 导入seleniumwire.undetected_chromedriver作为UC

chrome_options = uc.chromeoptions()

驱动程序= uc.Chrome(
    选项= chrome_options,
    seleniumwire_options = {}
)
 

Solved using Selenium-wire with undetected chromedriver.

Selenium Wire will integrate with undetected-chromedriver if it finds
it in your environment. This library will transparently modify
ChromeDriver to prevent it from triggering anti-bot measures on
websites.

If you wish to take advantage of this make sure you have
undetected_chromedriver installed:

pip install undetected-chromedriver

Then in your code, import the seleniumwire.undetected_chromedriver
package:

import seleniumwire.undetected_chromedriver as uc

chrome_options = uc.ChromeOptions()

driver = uc.Chrome(
    options=chrome_options,
    seleniumwire_options={}
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文