通过cookies信息模拟登陆

2020-02-10 22:56:41 浏览数 (1)

代码语言:javascript复制
 1 import requests
 2 
 3 # 这个练习演示的是通过传入cookie信息模拟登陆,这样操作的前提是需要预先在浏览器登陆账户抓包得到cookie字段信息
 4 
 5 url = "http://www.360wa.com/"
 6 headers1 = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"}
 7 
 8 headers2 = {'Cookie': 'cookie字符串',
 9             'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}
10 
11 cookie = cookie字符串
12 
13 # 这行代码的解释
14 # 首先for i in cookie.split(";") 意思是以分号分割遍历cookie字符串,并保存为变量i(由于cookie是字符串,所以遍历需要指定分隔符)
15 # 其次i.split("=")[0]:i.split("=")[1] 把遍历出来的字符串i再次以等号分割为两部分,索引为0的部分作为字典的key,索引为1的部分作为字典的value
16 cookie_dict = {i.split("=")[0]:i.split("=")[1] for i in cookie.split(";")}
17 print(cookie_dict)
18 
19 # 没有携带cookie信息
20 html1 = requests.get(url,headers=headers1)
21 with open("html1.html","w",encoding="utf-8") as f:
22     f.write(html1.content.decode())
23 
24 # 携带cookie信息,方法一,直接在headers里面携带
25 html2 = requests.get(url,headers=headers2)
26 with open("html2.html","w",encoding="utf-8") as f:
27     f.write(html2.content.decode())
28 
29 
30 # 携带cookie信息,方法二,单独传入cookies参数
31 html3 = requests.get(url,headers=headers1,cookies=cookie_dict)
32 with open("html3.html","w",encoding="utf-8") as f:
33     f.write(html3.content.decode())

0 人点赞