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

Status code 412 when trying to get response from chinese website

我在 returns 下的代码 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)

使用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={}
)