【问题标题】:How to retrieve data from a json如何从json中检索数据
【发布时间】:2020-07-05 19:34:48
【问题描述】:

我从新闻 API 中检索到 JSON 格式的数据集。我想从 JSON 数据中提取新闻描述。

这是我的代码:-

import requests
import json
url = ('http://newsapi.org/v2/top-headlines?'
       'country=us&'
       'apiKey=608bf565c67f4d99994c08d74db82f54')
response = requests.get(url)
di=response.json()
di = json.dumps(di)
for di['articles'] in di:
  print(article['title']) 

数据集如下所示:-

{'status': 'ok', 
 'totalResults': 38, 
 'articles': [
              {'source': 
                {'id': 'the-washington-post', 
                 'name': 'The Washington Post'}, 
               'author': 'Derek Hawkins, Marisa Iati', 
               'title': 'Coronavirus updates: Texas, Florida and Arizona officials say early reopenings fueled an explosion of cases - The Washington Post', 
               'description': 'Local officials in states with surging coronavirus cases issued dire warnings Sunday about the spread of infections, saying the virus was rapidly outpacing containment efforts.', 
               'url': 'https://www.washingtonpost.com/nation/2020/07/05/coronavirus-update-us/', 
               'urlToImage': 'https://www.washingtonpost.com/wp-apps/imrs.php?src=https://arc-anglerfish-washpost-prod-washpost.s3.amazonaws.com/public/K3UMAKF6OMI6VF6BNTYRN77CNQ.jpg&w=1440', 
               'publishedAt': '2020-07-05T18:32:44Z', 
               'content': 'Here are some significant developments:\r\n<ul><li>The rolling seven-day average for daily new cases in the United States reached a record high for the 27th day in a row, climbing to 48,606 on Sunday, … [+5333 chars]'}])

请指导我!

【问题讨论】:

  • 到目前为止你有什么尝试?什么问题?
  • 我在问题中提到了我的方法。
  • 我从新闻 API 中检索到 JSON 格式的数据集。我想从 JSON 数据中提取新闻描述。

标签: json python-3.x api data-analysis textblob


【解决方案1】:

您的代码中需要进行一些更正。下面的代码应该可以工作,并且我已经删除了 API KEY 以确保您在测试之前添加一个

import requests
import json
url = ('http://newsapi.org/v2/top-headlines?'
       'country=us&'
       'apiKey=<API KEY>')
di=response.json()
#You don't need to dump json that is already in json format
#di = json.dumps(di)
#your loop is not correctly defined, below is correct way to do it 
for article in di['articles']:
  print(article['title']) 

【讨论】:

    【解决方案2】:

    response.json

    {'status': 'ok', 
     'totalResults': 38, 
     'articles': [
                      {'source': 
                        {'id': 'the-washington-post', 
                         'name': 'The Washington Post'}, 
                       'author': 'Derek Hawkins, Marisa Iati', 
                       'title': 'Coronavirus updates: Texas, Florida and Arizona officials say early reopenings fueled an explosion of cases - The Washington Post', 
                       'description': 'Local officials in states with surging coronavirus cases issued dire warnings Sunday about the spread of infections, saying the virus was rapidly outpacing containment efforts.', 
                       'url': 'https://www.washingtonpost.com/nation/2020/07/05/coronavirus-update-us/', 
                       'urlToImage': 'https://www.washingtonpost.com/wp-apps/imrs.php?src=https://arc-anglerfish-washpost-prod-washpost.s3.amazonaws.com/public/K3UMAKF6OMI6VF6BNTYRN77CNQ.jpg&w=1440', 
                       'publishedAt': '2020-07-05T18:32:44Z', 
                       'content': 'Here are some significant developments:\r\n<ul><li>The rolling seven-day average for daily new cases in the United States reached a record high for the 27th day in a row, climbing to 48,606 on Sunday, … [+5333 chars]'}]}
    

    代码:

    di = response.json()   # Understand that 'di' is of type 'dictionary', key-value pair
    for i in di["articles"]:   
       print(i["description"])
    

    "articles" 是字典dikeys 之一,它对应的valuetype list"description" ,你正在寻找的是这个list (value of "articles") 的一部分。进一步的列表包含字典(key-value pair)。您可以从key - description访问

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-16
      • 1970-01-01
      • 1970-01-01
      • 2021-07-17
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多