NYtimes API, python
Question: Hi I want to get all the information after 'title' from NYtimes API, here is
my code

    
    
    from urllib2 import urlopen
    from json import loads
    import codecs
    import time
    
    def call_the_articles():
        url = "http://api.nytimes.com/svc/search/v1/article?query=US&facets=POLITICS&api-key=##"
        return loads(urlopen(url).read())
    
    articles = call_the_articles()
    
    if __name__ == '__main__':
    
    
        for story in articles("results"):
            print story['title'].encode('ascii', 'replace')
    

But when I run in terminal, the error coming out like:

    
    
    File "NYtimes.py", line 10, in <module>
        articles = call_the_articles()
      File "NYtimes.py", line 8, in call_the_articles
        return loads(urlopen(url).read())
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 126, in urlopen
        return _opener.open(url, data, timeout)
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 406, in open
        response = meth(req, response)
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 519, in http_response
        'http', request, response, code, msg, hdrs)
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 444, in error
        return self._call_chain(*args)
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 378, in _call_chain
        result = func(*args)
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 527, in http_error_default
        raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
    urllib2.HTTPError: HTTP Error 400: Bad Request
    

How to solve the problem?


Answer: I suspect what you want is:

    
    
    url = "http://api.nytimes.com/svc/search/v1/article?format=json&query=US+des_facet%3A%5BPOLITICS+AND+GOVERNMENT%5D&api-key=##
    

There are a couple of things going on to cause a bad request:

1.) You're using the `facets` keyword incorrectly. From the [Times API
developer docs on
facets](http://developer.nytimes.com/docs/read/article_search_api#h3-facets-
working):

> Facets can be thought of as search "perspectives." With facets, you can look
> at search results from different perspectives, and you can approach your
> search queries from different angles. Each facet can be seen as representing
> a property or characteristic of Times article data.
>
> Facets can reveal points of commonality and distinction that are not
> immediately apparent. For example, two articles with the word "bicycle" in
> the title may have two very different nytd_section_facet (NYTimes.com
> section) values: "Movies" and "Health." Similarly, two articles that discuss
> seemingly disparate topics, such as cloud computing and auto shows, may
> share a des_facet (descriptive subject term) value: "NEW MODELS, DESIGN AND
> PRODUCTS."

2.)You need to URLEncode your query when you send it through urlopen().

Also, `articles` will be a dict, so you'll want to get the articles out using
`[]`:

    
    
    for story in articles["results"]: 
    

If the query here is not exactly what you want, NYT has a tool that allows you
to play with constructing your queries: [NYT API Request
Tool](http://prototype.nytimes.com/gst/apitool/index.html?api_id=0&request_id=0&query=US%20des_facet%3a%5BPOLITICS%20AND%20GOVERNMENT%5D&facets=&begin_date=&fields=&offset=&rank=&resp_format=json&perform_request=Make%20Request&use_pp=on).

