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文件的部分内容:

原文地址:https://www.cnblogs.com/Maruying/p/13511982.html