python requests return a different web page from browser or urllib

不羁岁月 提交于 2019-12-07 00:12:31

After a lot of investigations I found that the site passes a cookie in the header attached to the first visitor to the site only.

so the solution is to get the cookies with head request, then resend them with your get request

import requests  
# get the cookies with head(), this doesn't get the body so it's FAST
cookies = requests.head('example.com')
# send get request with the cookies
result = requests.get('example.com', cookies=cookies)

Now It's faster than urllib + the same result :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!