requests-携带cookie信息发起请求

一、请求头携带cookie信息

# coding=utf-8
import requests
 
headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36",
    "Cookie":"anonymid=j3jxk555-nrn0wh; _r01_=1; _ga=GA1.2.1274811859.1497951251; _de=BF09EE3A28DED52E6B65F6A4705D973F1383380866D39FF5; ln_uact=mr_mao_hacker@163.com; depovince=BJ; jebecookies=54f5d0fd-9299-4bb4-801c-eefa4fd3012b|||||; JSESSIONID=abcI6TfWH4N4t_aWJnvdw; ick_login=4be198ce-1f9c-4eab-971d-48abfda70a50; p=0cbee3304bce1ede82a56e901916d0949; first_login_flag=1; ln_hurl=http://hdn.xnimg.cn/photos/hdn421/20171230/1635/main_JQzq_ae7b0000a8791986.jpg; t=79bdd322e760beae79c0b511b8c92a6b9; societyguester=79bdd322e760beae79c0b511b8c92a6b9; id=327550029; xnsid=2ac9a5d8; loginfrom=syshome; ch_id=10016; wp_fold=0"
}
# 在请求头中携带cookie信息。
 
r = requests.get("http://www.renren.com/327550029/profile", headers=headers)
 
#保存页面
with open("renren2.html","w",encoding="utf-8") as f:
    f.write(r.content.decode())

二、cookies参数指定cookie信息

# coding=utf-8
import requests
 
headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36",
}
 
# 该cookie字符串是登陆之后获取到的cookie
cookies="anonymid=j3jxk555-nrn0wh; _r01_=1; _ga=GA1.2.1274811859.1497951251; _de=BF09EE3A28DED52E6B65F6A4705D973F1383380866D39FF5; ln_uact=mr_mao_hacker@163.com; depovince=BJ; jebecookies=54f5d0fd-9299-4bb4-801c-eefa4fd3012b|||||; JSESSIONID=abcI6TfWH4N4t_aWJnvdw; ick_login=4be198ce-1f9c-4eab-971d-48abfda70a50; p=0cbee3304bce1ede82a56e901916d0949; first_login_flag=1; ln_hurl=http://hdn.xnimg.cn/photos/hdn421/20171230/1635/main_JQzq_ae7b0000a8791986.jpg; t=79bdd322e760beae79c0b511b8c92a6b9; societyguester=79bdd322e760beae79c0b511b8c92a6b9; id=327550029; xnsid=2ac9a5d8; loginfrom=syshome; ch_id=10016; wp_fold=0"
cookies = {i.split("=")[0]:i.split("=")[1] for i in cookies.split("; ")}  # (字典推导式) 将cookies字符串转换成字典
 
# 通过cookies参数携带cookie信息 (也可以通过headers请求头携带cookie信息)
r = requests.get("http://www.renren.com/327550029/profile", headers=headers, cookies=cookies)  # cookies参数接收字典类型
 
# 保存页面
with open("renren3.html","w",encoding="utf-8") as f:
    f.write(r.content.decode())

三、通过session会话对象发送请求

# coding=utf-8
import requests
 
session = requests.session()  # 实例化session会话对象
post_url = "http://www.renren.com/PLogin.do"
post_data = {"email":"用户名", "password":"登陆密码"}
headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36"
}
 
# 使用session发送post请求进行登陆。 自动保存cookie信息并携带cookie信息
session.post(post_url, data=post_data, headers=headers)
 
# 使用session进行请求  (自动携带cookie信息,自动登陆)
r = session.get("http://www.renren.com/327550029/profile", headers=headers)
 
# 保存页面
with open("renren1.html","w",encoding="utf-8") as f:
    f.write(r.content.decode())
posted @ 2021-05-30 15:03  SRE运维充电站  阅读(1147)  评论(0编辑  收藏  举报