在使用requests的过程中
一直是response 412,极其偶尔的情况下是200
请问这种情况有什么好的办法解决
网址贴在下面,是国家卫健委的网址,因为平时需要上去摘数据做日报
http://www.nhc.gov.cn/xcs/xxgzbd/gzbd_index.shtml
我只加了user-agent也会出现412的问题,加了cookie后就不会了,建议可以在浏览器中按F12,再访问url,查看network下该网站的请求头,把里面的cookie加到代码中去,如果不行也可以把其他的键值也加进去,这样应该没问题
给requests加上代理再去请求
连续运行了十次,全是200
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36',
'Host': 'www.nhc.gov.cn',
'Cookie': 'yfx_c_g_u_id_10006654=_ck22091516323913538784147391340; yfx_f_l_v_t_10006654=f_t_1663230759348__r_t_1663230759348__v_t_1663230759348__r_c_0; insert_cookie=91349450; security_session_verify=111cf453da2e821ce997a4726e5accbc'
}
url = 'http://www.nhc.gov.cn/xcs/xxgzbd/gzbd_index.shtml'
re = requests.get(url,headers=headers)
print(re.status_code)