json格式cookie转成可用的形式

使用edit this cookie导出的cookie是无法直接使用的所以需要转一下
以下是edit this cookie导出的cookie
cookies= [
{
"domain": ".jianshu.com",
"expirationDate": 1551985676.351818,
"hostOnly": false,
"httpOnly": true,
"name": "_m7e_session_core",
"path": "/",
"sameSite": "no_restriction",
"secure": true,
"session": false,
"storeId": "0",
"value": "915b30af3bea0a690d6b6a960322675d",
"id": 1
},
{
"domain": ".jianshu.com",
"hostOnly": false,
"httpOnly": false,
"name": "Hm_lpvt_0c0e9d9b1e7d617b3e6842e85b9fb068",
"path": "/",
"sameSite": "no_restriction",
"secure": false,
"session": true,
"storeId": "0",
"value": "1551963742",
"id": 2
},
{
"domain": ".jianshu.com",
"expirationDate": 1583499742,
"hostOnly": false,
"httpOnly": false,
"name": "Hm_lvt_0c0e9d9b1e7d617b3e6842e85b9fb068",
"path": "/",
"sameSite": "no_restriction",
"secure": false,
"session": false,
"storeId": "0",
"value": "1551189568,1551189734,1551708932,1551963705",
"id": 3
},
{
"domain": ".jianshu.com",
"expirationDate": 1553173330.383306,
"hostOnly": false,
"httpOnly": true,
"name": "remember_user_token",
"path": "/",
"sameSite": "no_restriction",
"secure": true,
"session": false,
"storeId": "0",
"value": "W1sxMzg3MzQ2MF0sIiQyYSQxMSRVVlNDWEVwNWc4dWZZZGRGQ096dXMuIiwiMTU1MTk2MzczMC4xMzk3MzciXQ%3D%3D--ddc058bf72cb61aee065900d3bd422f26026ec6c",
"id": 4
},
{
"domain": ".jianshu.com",
"expirationDate": 7859163742,
"hostOnly": false,
"httpOnly": false,
"name": "sensorsdata2015jssdkcross",
"path": "/",
"sameSite": "no_restriction",
"secure": false,
"session": false,
"storeId": "0",
"value": "%7B%22distinct_id%22%3A%221692a1a66b7606-083b483c2f27ac-1333063-1049088-1692a1a66b85d5%22%2C%22%24device_id%22%3A%221692a1a66b7606-083b483c2f27ac-1333063-1049088-1692a1a66b85d5%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E8%87%AA%E7%84%B6%E6%90%9C%E7%B4%A2%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22https%3A%2F%2Fwww.baidu.com%2Flink%22%2C%22%24latest_referrer_host%22%3A%22www.baidu.com%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%7D%2C%22first_id%22%3A%22%22%7D",
"id": 5
},
{
"domain": "www.jianshu.com",
"expirationDate": 1582725568,
"hostOnly": true,
"httpOnly": false,
"name": "__yadk_uid",
"path": "/",
"sameSite": "no_restriction",
"secure": false,
"session": false,
"storeId": "0",
"value": "2hrhXccCAbHk9F5hBrUu9jl5auvIlVwU",
"id": 6
},
{
"domain": "www.jianshu.com",
"hostOnly": true,
"httpOnly": false,
"name": "default_font",
"path": "/",
"sameSite": "no_restriction",
"secure": false,
"session": true,
"storeId": "0",
"value": "font2",
"id": 7
},
{
"domain": "www.jianshu.com",
"hostOnly": true,
"httpOnly": false,
"name": "locale",
"path": "/",
"sameSite": "no_restriction",
"secure": false,
"session": true,
"storeId": "0",
"value": "zh-CN",
"id": 8
},
{
"domain": "www.jianshu.com",
"hostOnly": true,
"httpOnly": false,
"name": "read_mode",
"path": "/",
"sameSite": "no_restriction",
"secure": false,
"session": true,
"storeId": "0",
"value": "day",
"id": 9
},
{
"domain": "www.jianshu.com",
"hostOnly": true,
"httpOnly": false,
"name": "signin_redirect",
"path": "/",
"sameSite": "no_restriction",
"secure": false,
"session": true,
"storeId": "0",
"value": "https%3A%2F%2Fwww.jianshu.com%2F",
"id": 10
}
]
转换

import http.cookiejar

cookiejar = http.cookiejar.CookieJar()  
for cookie in cookies:  
    cookiejar.set_cookie(  
        http.cookiejar.Cookie(version=0, name=cookie['name'], value=cookie['value'], port=None, port_specified=False,  
                         domain=cookie['domain'], domain_specified=False, domain_initial_dot=False, path=cookie['path'],  
                         path_specified=True, secure=cookie['secure'], expires=None, discard=True, comment=None,  
                         comment_url=None, rest={'HttpOnly': None}, rfc2109=False))

# print(cookiejar)

res = requests.get(url=url,headers=headers,cookies=cookiejar)  
print(res.text)
原文地址:https://www.cnblogs.com/zhangxuel1ang/p/14257747.html