import datetimearticles = nyt.article_search( query ="Obama", # Search for articles about Obama results =30, # Return 30 articles# Search for articles in January and February 2019 dates = {"begin": datetime.datetime(2019, 1, 31),"end": datetime.datetime(2019, 2, 28) }, options = {"sort": "oldest", # Sort by oldest options# Return articles from the following four sources"sources": ["New York Times","AP","Reuters","International Herald Tribune" ],# Only get information from the Politics desk"news_desk": ["Politics" ],# Only return News Analyses"type_of_material": ["News Analysis" ],# The article text should contain either the word# "Obamacare" or "healthcare""body": ["Obamacare","healthcare" ],# Headline should contain "bill", "costs over", or "victory""headline": ["bill","costs over","victory" ] })
Advanced Usage: Filter Queries
The New York Times article search API allows for many more filters, which can be combined much more specifically. You can specify a filter query be specifying the fq key of the options parameter. This can be done as follows:
# Get all sports articles from New York Cityarticles = nyt.article_search(options={"fq": "news_desk:("Sports") AND glocations:(\"NEW YORK CITY\")"})# Get all articles about Obama, but exclude all# articles from New York Cityarticles = nyt.article_search(options={"fq": "Obama AND -glocations:(\"NEW YORK CITY\")"})