if __name__ =='__main__':
resp = requests.get("http://www.landchina.com/", timeout=5)print("info:")print(resp.text)print("cookies:")for key, value in resp.cookies.items():print(f"key: {key} value: {value}")
仔细观察发现请求返回的html结果中有如下代码
functionstringToHex(str){var val ="";for(var i =0; i < str.length; i++){if(val =="") val = str.charCodeAt(i).toString(16);else val += str.charCodeAt(i).toString(16);}return val;}functionYunSuoAutoJump(){var width = screen.width;var height = screen.height;var screendate = width +","+ height;var curlocation = window.location.href;if(-1== curlocation.indexOf("security_verify_")){
document.cookie ="srcurl="+stringToHex(window.location.href)+";path=/;";}
self.location ="/?security_verify_data="+stringToHex(screendate);}setTimeout("YunSuoAutoJump()",50);
if __name__ =='__main__':
url ="http://www.landchina.com/"
resp = requests.get(url, timeout=5)
cookie ={}for key, value in resp.cookies.items():
cookie[key]= value
resp = requests.get('{}{}'.format(url,'?security_verify_data=313932302c31303830'),
cookies=cookie
)print(resp.text)for key, value in resp.cookies.items():
cookie[key]= value
print(f'{key}: {value}')
html 内容和第一次请求一样依旧不是正确结果,不过又返回了一个cookie: security_session_mid_verify: b621fcf2ed74e709c200e2af37eada46 3. 看来还要再携带返回的cookie进行一次请求,修改代码如下:
if __name__ =='__main__':
url ="http://www.landchina.com/"
resp = requests.get(url, timeout=5)
cookie ={}for key, value in resp.cookies.items():
cookie[key]= value
resp = requests.get('{}{}'.format(url,'?security_verify_data=313932302c31303830'),
cookies=cookie
)for key, value in resp.cookies.items():
cookie[key]= value
# 注意这里的cookie字典中存有第一次请求得到的 yunsuo_session_verify,和第二次请求得到的 security_session_mid_verify
resp = requests.get(
url,
cookies=cookie
)print(resp.status_code)print(resp.text)