Skip to content

Instantly share code, notes, and snippets.

@NanoDano
Created October 13, 2016 03:38
Show Gist options
  • Save NanoDano/9b551c40eab4226ed85bd065780a8029 to your computer and use it in GitHub Desktop.
Save NanoDano/9b551c40eab4226ed85bd065780a8029 to your computer and use it in GitHub Desktop.
Use the NYTimes API to query for articles in Python
import requests
import json
import time
MY_API_KEY = 'xxxx11234789012347891023478xxxxx'
def get_articles(api_key, category):
for i in range(0, 5):
url = 'http://api.nytimes.com/svc/search/v2/articlesearch.json'
url += '?/api-key=%s&fq=news_desk:("%s")&page=%s' % (api_key, category, i)
print(url)
response = requests.get(url)
print(response.headers)
time.sleep(1)
print(response.text)
data = json.loads(response.text)
print(data)
for meta in data['response']['meta']:
print(meta)
for result in data['response']['docs']:
print(result)
get_articles(MY_API_KEY, "Sports")
get_articles(MY_API_KEY, "Arts")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment