1. 程式人生 > 實用技巧 >python中使用cookie進行模擬登入

python中使用cookie進行模擬登入

背景:使用cookie模擬登入豆瓣->我的豆瓣網頁

準備工作

1、通過Fiddler抓取“我的豆瓣”url;

2、通過Fiddler抓取“我的豆瓣”cookie值。

import urllib3
import requests
urllib3.disable_warnings()

# Fiddler抓取到的URL和Cookie值
url = "https://www.douban.com/people/xxxxxx/"
Cookie = 'll="118282"; bid=H9R4ffHOABM; _pk_ref.100001.8cb4=%5B%22%22%2C%22%22%2C1597555195%2C%22https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DBnetNO3T-xxxxxx
%26wd%3D%26eqid%3Ddc59be8e0003f343000000065f389d26%22%5D; _pk_id.100001.8cb4=6729c2b423e4bbbb.1597545769.3.1597555195.1597551197.; __utma=30149280.1595459493.1597545770.1597551198.1597555195.3; __utmc=30149280; __utmz=30149280.1597545770.1.1.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; push_noty_num=0; push_doumail_num=0; __utmv=30149280.22151; __yadk_uid=xxxxxx
; douban-profile-remind=1; dbcl2="xxxxxx:TmEAQUbm6uQ"; ck=qJYs; _pk_ses.100001.8cb4=*; ap_v=0,6.0; __utmb=30149280.2.10.1597555195; __utmt=1
' header = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Firefox/68.0", 'Cookie': Cookie # 將Cookie值新增到header請求頭中 } # session = requests.session()
response = requests.get(url=url, headers=header, verify=False) try: # 增加斷言,判斷是否模擬登入成功 assert "Maruying" in response.text assert "我的主頁" in response.text except Exception as e: raise e else: with open("douban1.html", 'w', encoding='utf-8')as file: file.write(response.text)

douban1.html檔案的部分內容: