Monday, September 29, 2014

Consuming New York Times API with python

Hi,

I didn't know earlier that New York Times actually has an api for accessing it's resources which are even as old as 1851! Tons of articles, columns, news can be mined quite easily. The steps involved in their consumption is nothing of a complex sort. Just obtain a key of each type of resource you want to access and call the api with it.
Below is an example of accessing it with python.

1. Get the key from this link: http://developer.nytimes.com/docs
2. Install this module: https://pypi.python.org/pypi/nytimesarticle/0.1.0
3. Learn about filtering the search: http://developer.nytimes.com/docs/read/article_search_api_v2#filters

Now this is a simple example of getting all the articles by Roger Cohen, one of my favorite writers, which includes issues on 'usa'.

from nytimesarticle import articleAPI
api = articleAPI('your_article_access_token')
res = api.search( q = 'usa', fq = {'byline':'ROGER COHEN', 'source':['The New York Times']}, begin_date = 20140901, facet_field = ['source','day_of_week'], facet_filter = True )
for m in res['response']['docs']:
    print m['web_url']

No comments:

Post a Comment