[text] test213

Viewer

  1. import requests
  2. import pandas as pd
  3.  
  4.  
  5. baseUrl = "https://api.nytimes.com/svc/search/v2/articlesearch.json"
  6. apiKey = 'api-key=JFGi0EjwEBOF9smKyLcGyrUrtYAhzJEn'
  7. searchQuery = 'q=covid'
  8. page = 0
  9. filterVal = "fq=headline:(\"covid\") AND pub_year:(2020)"
  10.  
  11. apiURL = f"{baseUrl}?{searchQuery}&{apiKey}&{filterVal}&{page}"
  12. arrNewsHead = []
  13.  
  14. while page < 5:
  15.     apiURL = f"{baseUrl}?{searchQuery}&{apiKey}&{filterVal}&page={page}"
  16.  
  17.     print(apiURL)
  18.  
  19.     result = requests.get(apiURL)
  20.     news = result.json()
  21.  
  22.     for p in news["response"]["docs"]:
  23.         print(p["headline"]["main"])
  24.         arrNewsHead.append(p["headline"]["main"])
  25.         print('---')
  26.     page += 1
  27.  
  28. df_NewsText = pd.DataFrame(arrNewsHead, columns=["NewsTextHead"])
  29. print(df_NewsText)

Editor

You can edit this paste and save as new:


File Description
  • test213
  • Paste Code
  • 22 Oct-2020
  • 776 Bytes
You can Share it: