1. burpsuit獲取登錄知乎的cookie
登錄知乎后怀偷,打開burpsuit假抄,刷新知乎頁(yè)面獲取cookie,并將cookie保存到本地
2. firefox清除cookie
清楚firefox中的cookie,并重新打開https://www.zhihu.com ,查看已退出知乎賬號(hào)
3. 驗(yàn)證cookie的可用性
隨意進(jìn)入一個(gè)知乎的回答問(wèn)題頁(yè)面德谅,用burpsuit將當(dāng)前cookie修改為之前保存的cookie爹橱,接下來(lái)一只Forward
4. 利用保存的cookie實(shí)現(xiàn)模擬登錄
#-*- coding: UTF-8 -*-
import requests
import re
#將cookies轉(zhuǎn)換成字典形式萨螺,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
#將獲取到的頁(yè)面源碼寫入zhihu.html文件中
with open('zhihu.html','w') as fl:
fl.write(html)