Created
October 13, 2016 03:38
-
-
Save NanoDano/9b551c40eab4226ed85bd065780a8029 to your computer and use it in GitHub Desktop.
Use the NYTimes API to query for articles in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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