- 把cookie先写成字典形式,然后把字典转换为cookiejar
import requests
from requests.cookies import cookiejar_from_dict
# 注意:这个方法会替换掉原有的cookies
# 开启一个会话Session
session = requests.Session()
# 从chrome浏览器中取到的cookie值
cookie_dict = {
"49BAC005-7D5B-4231-8CEA-16939BEACD67": "cktest001",
"JSESSIONID": "F4FFF69B8XXXXXXC8DCB4C061C0",
"JSESSIONIDSSO": "9D49C76FD6XXXXXF294242B44A",
}
# 把cookie值转换为cookiejar类型,然后传给Session
session.cookies = cookiejar_from_dict(cookie_dict, cookiejar=None, overwrite=True)
print(session.cookies)
import requests
from requests.cookies import RequestsCookieJar
# 开启一个会话Session
session = requests.Session()
# 创建一个Cookie Jar对象
jar = RequestsCookieJar()
# 向Cookie Jar对象中添加cookie值
jar.set("49BAC005-7D5B-4231-8CEA-1XXXXBEACD67", "cktXXXX001")
jar.set("JSESSIONID", "F4FFF69B8CXXXX80F0C8DCB4C061C0")
jar.set("JSESSIONIDSSO", "9D49C7XXXX448FDF5B0F294242B44A")
# 把cookies追加到Session中
session.cookies.update(jar)
print(session.cookies)
文章评论