Skip to content

Instantly share code, notes, and snippets.

@katsully
Created March 25, 2015 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katsully/36f0338fa0b41d088db0 to your computer and use it in GitHub Desktop.
Save katsully/36f0338fa0b41d088db0 to your computer and use it in GitHub Desktop.
import requests
from random import randint
counter = 0
term = "Right-Wing"
while counter < 20:
r = requests.get('http://api.nytimes.com/svc/search/v2/articlesearch.json?fq=headline:(' + term + ')&api-key=8507eaad79faa9ac49204db270a8e0e3:13:70195569')
if r.ok == False:
break
data = r.json()
docs = data['response']['docs']
found_article = False
headline = ""
for doc in docs:
headline = doc['headline']['main']
if headline.rsplit(' ')[0] == term:
found_article = True
break
counter2 = 1
while found_article is False:
r = requests.get('http://api.nytimes.com/svc/search/v2/articlesearch.json?fq=headline:(' + term + ')&page='+str(counter2)+'&api-key=8507eaad79faa9ac49204db270a8e0e3:13:70195569')
if r.ok == False:
break
data = r.json()
docs = data['response']['docs']
found_article = False
headline = ""
for doc in docs:
headline = doc['headline']['main']
if headline.rsplit(' ')[0] == term:
found_article = True
break
counter2 += 1
if counter2 > 15 :
break
if found_article is False:
break
print headline.encode('utf-8')
term = headline.rsplit(' ')[-1]
if term.find('-') !=-1:
term = term.rsplit('-')[-1]
counter += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment