简体   繁体   English

从 json 对象中检索数据

[英]Retrieving data from a json object

I am writing a parser to extract the list of ads我正在编写一个解析器来提取广告列表

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')
   })

At the moment, I get the following output.目前,我得到以下输出。 I need to make my conclusion shorter.我需要缩短我的结论。

[
    {
        "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"
    }
]

I would like to bring to the form:我想带上表格:

    "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

How can I extract information from owner{.id} and images[.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)

You could replace:-你可以替换:-

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

...with... ...和...

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

You could do it by only extracting the information you want:你可以只提取你想要的信息来做到这一点:

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')
   })

Result:结果:

[{'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