at present, I search for Wechat"s articles through Sogou"s Wechat search, the code is as follows:
def get_html(url):
headers = {
"Host": "weixin.sogou.com",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
"Accept-Encoding": "gzip, deflate",
"Referer": "http://weixin.sogou.com/weixin?type=2&ie=utf8&query=%E6%A2%85%E8%A5%BF&tsn=2&ft=&et=&interation=&wxid=&usip=",
"Connection": "keep-alive",
"Upgrade-Insecure-Requests": "1",
"Pragma": "no-cache",
"Cache-Control": "no-cache"
}
r = requests.get(url=url, headers=headers)
html = r.text
return html
url = "http://weixin.sogou.com/weixin?type=2&ie=utf8&query=%E6%A2%85%E8%A5%BF&tsn=5&ft=2018-05-05&et=2018-06-05&interation=&wxid=&usip="
print(get_html(url=url))
but when the above code is executed, 302 will be redirected to http://weixin.sogou.com/
for the second time. I bring cookie to visit, and the result is the same.
this is how to get cookie:
def get_cookie(timeout=30):
url = "http://weixin.sogou.com/weixin?type=2&s_from=input&query=%E5%BE%AE%E4%BF%A1%E5%85%AC%E4%BC%97%E5%8F%B7&ie=utf8&_sug_=y&_sug_type_=&w=01019900&sut=13322&sst0=1524114483262&lkt=13%2C1524114471294%2C1524114483150"
headers = {
"User-Agent": random.choice(USER_AGENTS),
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Language": "zh-CN,zh;q=0.8",
"Accept-Encoding": "gzip, deflate, sdch",
"DNT": "1",
"Connection": "keep-alive",
}
r = requests.get(url=url, headers=headers, timeout=timeout)
if "set-cookie" in r.headers:
cookie = r.headers["set-cookie"]
else:
cookie = ""
return cookie