import requests my_times_api_key = 'YOUR-API-GOES-HERE' payload = {'q' : 'inequality', 'begin_date': '20130101' , 'end_date' : '20131231', 'api-key' : my_times_api_key, 'sort' : 'oldest' , 'offset' : 20} base_url = 'http://api.nytimes.com/svc/search/v2/articlesearch.json?' r = requests.get(base_url, params = payload) r.url r.text r.json() json = r.json() json.keys() json.keys() json['status'] json['response']['docs'] json['response']['meta']['hits'] from time import sleep base_url = 'http://api.nytimes.com/svc/search/v2/articlesearch.json?' years = [2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013] for year in years: year_string = str(year) payload = { 'q' : 'inequality', 'begin_date': year_string + '0101', 'end_date' : year_string + '1231', 'api-key' : my_times_api_key, 'sort' : 'newest' , 'page' : 0} r = requests.get(base_url, params = payload) json = r.json() count = json['response']['meta']['hits'] print year, count sleep(.1) import csv outfile = open('inequality_annual.csv', 'w') csv_writer = csv.writer(outfile) header = ['year','article_count'] csv_writer.writerow(header) base_url = 'http://api.nytimes.com/svc/search/v2/articlesearch.json?' years = [2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013] for year in years: year_string = str(year) payload = { 'q' : 'inequality', 'begin_date': year_string + '0101', 'end_date' : year_string + '1231', 'api-key' : my_times_api_key, 'sort' : 'newest' , 'page' : 0} sleep(.1) outfile.close() !cat inequality.csv from time import sleep base_url = 'http://api.nytimes.com/svc/search/v2/articlesearch.json?' payload = { 'q' : 'inequality', 'api-key' : my_times_api_key, 'sort' : 'newest' , 'page' : 0} years = [2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013] counts = [] for year in years: year_string = str(year) payload['begin_date'] = year_string + '0101' payload['end_date'] = year_string + '1231' r = requests.get(base_url, params = payload) json = r.json() count = json['response']['meta']['hits'] counts.append(count) sleep(.1) %pylab inline import matplotlib.pyplot as plt plt.scatter(years,counts) plt.ticklabel_format(useOffset=False) outfile = open('inequality_annual.csv', 'w') csv_writer = csv.writer(outfile) base_url = 'http://api.nytimes.com/svc/search/v2/articlesearch.json?' def last_day_calculator(month): '''A little function that tells you the last day of the month.''' if month in [1,3,5,7,8,10,13]: last_day = '31' elif month in [4,6,9,11]: last_day = '30' else: last_day = '28' return last_day #Loop through each year 2006-2013 for year in range(2006,2013): #turn the interger into string year_string = str(year) #Loop through each month for month in range(1,3): #figure out the last day of that month last_day_string = last_day_calculator(month) month_string = "%02d" % month payload = { 'q' : 'inequality', 'begin_date': year_string + month_string + '01', 'end_date' : year_string + month_string + last_day_string, 'api-key' : my_times_api_key, 'sort' : 'newest' , 'page' : 0} r = requests.get(base_url, params = payload) json = r.json() count = json['response']['meta']['hits'] csv_writer.writerow( [year_string, month_string, count] ) sleep(.5) outfile.close() !cat inequality_annual.csv