简体   繁体   English

如何仅从 json 文件中的 cookies 中提取某些值?

[英]How to extract only certain values from cookies in a json file?

Iam gathering cookies from a selenium browser and then writing them to a json file.我从 selenium 浏览器收集 cookies,然后将它们写入 json 文件。 I want to then read them and only extract the name and value keys and values.然后我想阅读它们,只提取namevalue键和值。 However, I dont know how to convert them back into a dictionary without changing the cookies in any way, which I see as a common answer on here.但是,我不知道如何在不以任何方式更改 cookies 的情况下将它们转换回字典,我认为这是这里的常见答案。

Here are the cookies below下面是cookies

[{"domain": ".instagram.com", "httpOnly": true, "name": "rur", "path": "/", "secure": true, "value": "PRN"}, {"domain": ".instagram.com", "expiry": 1610372060, "httpOnly": true, "name": "shbid", "path": "/", "secure": true, "value": "2630"}, {"domain": ".instagram.com", "expiry": 1610372060, "httpOnly": true, "name": "shbts", "path": "/", "secure": true, "value": "1609767261.111102"}, {"domain": ".instagram.com", "expiry": 1641303259, "httpOnly": true, "name": "sessionid", "path": "/", "secure": true, "value": "5973912167%3A79EfmCoMj2hdDd%3A29"}, {"domain": ".instagram.com", "expiry": 1641216860, "httpOnly": false, "name": "csrftoken", "path": "/", "secure": true, "value": "z1i7aU8fUnFkO7jXS8eOcVzmzQVfCFTP"}, {"domain": ".instagram.com", "expiry": 1672839253, "httpOnly": false, "name": "mid", "path": "/", "secure": true, "value": "X_MZVgALAAFI1JXJOTF_ZG-E-Cny"}, {"domain": ".instagram.com", "expiry": 1617543260, "httpOnly": false, "name": "ds_user_id", "path": "/", "secure": true, "value": "5973912167"}, {"domain": ".instagram.com", "expiry": 1672839253, "httpOnly": true, "name": "ig_did", "path": "/", "secure": true, "value": "AD703D6F-E24A-4485-A1EC-9736E40C19C3"}, {"domain": ".instagram.com", "httpOnly": true, "name": "urlgen", "path": "/", "secure": true, "value": "\"{\\\"86.15.149.131\\\": 5089}:1kwQ05:r7Bsx0VcOwuIw_rejyFuGdmgAIo\""}, {"domain": "www.instagram.com", "expiry": 4765462453, "httpOnly": false, "name": "ig_cb", "path": "/", "secure": false, "value": "2"}]

I don't really know if this is exactly what you want but I extracted all the domain and value keys and value and put them back in a dictionary and put all the dictionary in a list我真的不知道这是否正是您想要的,但我提取了所有域和值键和值并将它们放回字典中并将所有字典放入列表中

 var = *YOUR COOKIES LIST*

liste = []
for i in range(len(var)) : 
        #print(var[i]["domain"])
        #print(var[i]["value"])
        dic = {"domain":var[i]["domain"],"value":var[i]["value"]}
        liste.append(dic)
        
        
print(liste)

maybe this would have since you saved it as a file:也许这是因为您将其保存为文件:

import json
with open('data.json', 'r') as f:
    datas = json.load(f)
    
    for data in datas:
        print(data["name"], " : ", data["value"])

resulting in导致

rur  :  PRN
shbid  :  2630
shbts  :  1609767261.111102
sessionid  :  5973912167%3A79EfmCoMj2hdDd%3A29
csrftoken  :  z1i7aU8fUnFkO7jXS8eOcVzmzQVfCFTP
mid  :  X_MZVgALAAFI1JXJOTF_ZG-E-Cny
ds_user_id  :  5973912167
ig_did  :  AD703D6F-E24A-4485-A1EC-9736E40C19C3
urlgen  :  "{\"86.15.149.131\": 5089}:1kwQ05:r7Bsx0VcOwuIw_rejyFuGdmgAIo"
ig_cb  :  2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM