Passing multiple keywords through an HTTP request with NewsAPI.org

EDIT: For anyone who stumbles across this with the same problem, it turns out the top_headlines doesn't support multiple keywords, which is a bummer. Here's the working code for multiple keywords with "everything" instead of "top headlines".

keywords = ["warriors", "spurs"]

url = ('https://newsapi.org/v2/everything?q=' + ' OR '.join(keywords)) + '&language=en' + '&apiKey=' + api_key + '&pageSize=100'

response = requests.get(url)

This returns A LOT, because, you know, 'everything'. Look into the 'from' and 'to' parameters to limit the return a bit.

OP Starts Here


I'm currently trying to get the top articles that contain certain keywords from the news aggregate NewsAPI.org.

If I only feed it one keyword, it works flawlessly, example code of this below:

keywords = ["trump"]

url = ('https://newsapi.org/v2/top-headlines?q=' + ','.join(keywords)) + '&language=en' + '&apiKey=' + api_key + '&pageSize=100'

response = requests.get(url)

for article in response.json()['articles']:
    print(article['title'])

print(url)

print(response.json()['totalResults'])

But for the life of me, I cannot figure out how to pass multiple keywords to this request. I've tried every variation of joining the list and it just won't cooperate.

The goal is to be able to put another string in the keywords list, for example:

keywords = ["trump","bitcoin"]

And then for the code to return all headlines that include either trump OR bitcoin. The closest I've gotten is returning headlines that include both the keywords, but I can't get it to return one or the other.

I've tried joining these strings with every strategy I could imagine ('+', ' ', '&', '&q=', etc) and I just can't figure it out. Not sure what to try from here. Any tips?


Solution 1:

The News API documentation shows a way to do this for the "Everything" endpoint:

Advanced search is supported here:

...

  • Alternatively you can use the AND / OR / NOT keywords, and optionally group these with parenthesis. Eg: crypto AND (ethereum OR litecoin) NOT bitcoin.

The complete value for q must be URL-encoded.

There is no such information in the docs for "Top Headlines", which would seem to imply that advanced search is not supported for the "Top Headlines" endpoint.