Search code examples
jsonpython-3.xapidata-analysistextblob

How to retrieve data from a json


I retrieved a dataset from a news API in JSON format. I want to extract the news description from the JSON data.

This is my code:-

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

The dataset looks like this:-

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

Please guide me with this!


Solution

  • There are few corrections needed in your code.. below code should work and i have removed API KEY in answer make sure that you add one before testing

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