1. burpsuit获取登录知乎的cookie
登录知乎后,打开burpsuit,刷新知乎页面获取cookie,并将cookie保存到本地
2. firefox清除cookie
清楚firefox中的cookie,并重新打开https://www.zhihu.com ,查看已退出知乎账号
3. 验证cookie的可用性
随意进入一个知乎的回答问题页面,用burpsuit将当前cookie修改为之前保存的cookie,接下来一只Forward
4. 利用保存的cookie实现模拟登录
#-*- coding: UTF-8 -*-
import requests
import re
#将cookies转换成字典形式,zhihu_cookie为保存的cookie文件,跟程序处在同一路径
def get_cookie():
with open('zhihu_cookie','r') as f:
cookies={}
for line in f.read().split(';'):
name,value=line.strip().split('=',1) #1代表只分割一次
cookies[name]=value
return cookies
s = requests.Session()
url = 'http://www.zhihu.com/#signin'
headers = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36',
'Accept':'*/*',
'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding':'gzip, deflate, br',
'Referer':'https://www.zhihu.com/'
}
req2 = s.get(url, headers = headers, cookies = get_cookie(), verify=False)
html = req2.content
#将获取到的页面源码写入zhihu.html文件中
with open('zhihu.html','w') as fl:
fl.write(html)