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



下面的代码返回<响应[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)

使用硒线和未检测到的镀铬器解决。

如果Selenium Wire发现它在您的环境中。此库将透明地修改ChromeDriver防止其在网站。

如果你想利用这一点,请确保安装了未检测到的彩色打印机:

pip install undetected-chromedriver

然后在您的代码中,导入seleniumwire.undetectedchromedriver包装:

import seleniumwire.undetected_chromedriver as uc
chrome_options = uc.ChromeOptions()
driver = uc.Chrome(
options=chrome_options,
seleniumwire_options={}
)

相关内容

最新更新