簡體   English   中英

從 json 對象中檢索數據

[英]Retrieving data from a json object

我正在編寫一個解析器來提取廣告列表

response = requests.get(url).json()

items = response['data']
iter1 = []
for item in items:
   iter1.append({
     'name': item.get('name', 'NA'),
     'owner': item.get('owner', 'NA'),
     'date_published': item.get('date_published', 'NA'),
     'images': item.get('images', 'NA'),
     'short_url': item.get('short_url', 'NA')
   })

目前,我得到以下輸出。 我需要縮短我的結論。

[
    {
        "name": "Announcement name",
        "owner": {
            "id": "58f84949700743"
        },
        "date_published": 1627666233,
        "images": [
            {
                "id": "58fb7032ca5544fb5a2",
                "num": 1,
                "url": "https://cache3.com/images/orig/58/fb/58fb70f2132a554804fb5a2.jpg",
                "width": 1936,
                "height": 2581
            },
            {
                "id": "58fb70f29e94ba0384507554",
                "num": 2,
                "url": "https://cache3.com/images/orig/58/fb/58fb70f29e94b384507554.jpg",
                "width": 750,
                "height": 1334
            },
            {
                "id": "58fb70f2f8efdc109d76c2e5",
                "num": 3,
                "url": "https://cache3.com/images/orig/58/fb/58fb70f2fdc109d76c2e5.jpg",
                "width": 750,
                "height": 1334
            }
        ],
        "short_url": "https://short.com/p58gb7b9a4c80320f03"
    }
]

我想帶上表格:

    "name": "Announcement name", #Name
        "id": "58f84949700743" #Owner ID
    "date_published": 1627666233, #Date
            "url": "https://cache3.com/images/orig/58/fb/58fb70f2132a554804fb5a2.jpg",#Url-img
    "short_url": "https://short.com/p58gb7b9a4c80320f03" #Announcement url

如何從 owner{.id} 和 images[.url] 中提取信息?

dict=[ { "name": "Announcement name", "owner": { "id": "58f84949700743" }, "date_published": 1627666233, "images": [ { "id": "58fb7032ca5544fb5a2", "num": 1, "url": "https://cache3.com/images/orig/58/fb/58fb70f2132a554804fb5a2.jpg", "width": 1936, "height": 2581 }, { "id": "58fb70f29e94ba0384507554", "num": 2, "url": "https://cache3.com/images/orig/58/fb/58fb70f29e94b384507554.jpg", "width": 750, "height": 1334 }, { "id": "58fb70f2f8efdc109d76c2e5", "num": 3, "url": "https://cache3.com/images/orig/58/fb/58fb70f2fdc109d76c2e5.jpg", "width": 750, "height": 1334 } ], "short_url": "https://short.com/p58gb7b9a4c80320f03" } ]

result = {}
result["name"] = dict[0].get("name", 'NA')
result["id"] = dict[0].get('owner', {}).get('id', 'NA')
result["date_published"] = dict[0].get("date_published", 'NA')
result["url"] = []
result["short_url"] = dict[0].get("short_url", 'NA')

for img in dict[0].get("images", []):
    if "url" in img:
        result["url"].append(img["url"])
    
print(result)

你可以替換:-

'owner': item.get('owner', 'NA'),

...和...

'id': item.get('owner', {}).get('id', 'NA'),

你可以只提取你想要的信息來做到這一點:

items = response['data']
iter1 = []
for item in items:
   iter1.append({
     'name': item.get('name', 'NA'),
     'id': item.get('owner', {}).get('id', 'NA'),
     'date_published': item.get('date_published', 'NA'),
     'urls': [entry.get('url', 'NA') for entry in item.get('images', [])],
     'short_url': item.get('short_url', 'NA')
   })

結果:

[{'name': 'Announcement name',
  'id': '58f84949700743',
  'date_published': 1627666233,
  'urls': ['https://cache3.com/images/orig/58/fb/58fb70f2132a554804fb5a2.jpg',
           'https://cache3.com/images/orig/58/fb/58fb70f29e94b384507554.jpg',
           'https://cache3.com/images/orig/58/fb/58fb70f2fdc109d76c2e5.jpg'],
  'short_url': 'https://short.com/p58gb7b9a4c80320f03'}]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM