python requests cookie格式转换

import requests


# 1.查看cookie
# 2.对比cookie
# cookies = [{'domain': '.mingrisoft.com',
#             'httpOnly': False,
#             'name': 'PHPSESSID',
#             'path': '/',
#             'secure': True,
#             'value': 'om82p1kbh2kqfhbe97qhmb9l64'}]

# 给requests请求添加cookie

url = "https://www.douban.com/people/170209839/"
cookies = 'll="108288"; bid=NodlXzfaH_o; __gads=ID=0af91ce932c6d12c-22c34cfa8fc90047:T=1624027452:RT=1624027452:S=ALNI_MYs2I1EtkmfR1_-nvFPUE58Xuzvwg; douban-fav-remind=1; viewed="35118043"; gr_user_id=bbba787a-8040-4d2a-99c9-0e7205d55631; __utmz=30149280.1628424734.8.6.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; __utmc=30149280; ap_v=0,6.0; push_doumail_num=0; push_noty_num=0; __utmv=30149280.17020; __yadk_uid=Uk2xTm3EEqSEfelnBcfkvNx941OBBMdi; dbcl2="170209839:UMNN1t0Q5hc"; ck=EP97; _pk_ref.100001.8cb4=%5B%22%22%2C%22%22%2C1628429455%2C%22https%3A%2F%2Faccounts.douban.com%2F%22%5D; _pk_ses.100001.8cb4=*; __utma=30149280.1778247111.1624027427.1628424734.1628429495.9; __utmt=1; _pk_id.100001.8cb4=9e98f5fdb9dc0e4d.1624027404.6.1628429843.1628427334.; __utmb=30149280.6.10.1628429495'
headers = {
    'Host': 'www.douban.com',
    'Referer': 'https://www.douban.com/',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 Edg/92.0.902.67'
}
cookies_jar = requests.cookies.RequestsCookieJar()
for cookie in cookies.split(';'):
    key, value = cookie.split('=', 1)
    cookies_jar.set(key, value)
res = requests.get(url, headers=headers,cookies=cookies_jar)
if res.status_code == 200:
    print(res.text)

来源:有道云笔记 (youdao.com)

posted on 2023-12-22 10:29  valin  阅读(213)  评论(0编辑  收藏  举报