Python script to search for News based on keywords | Daily Python #5
This article is a tutorial on how to search for news based on given keywords using news API in Python.
This article is a part of Daily Python challenge that I have taken up for myself. I will be writing short python articles daily.
Requirements:
- Python 3.0
- Pip
Install the following packages:
- newsapi — Library to fetch news from various news sources.
pip install newsapi-python
Let’s visit the newsapi.org, to understand how its API works, and how we can get one for our python script.
News API searches for articles from over 30,000 news sources and blogs. It’s a huge number and it’s not easy to imagine. You need to create an account on News API in order to generate an API key for your application/program.
After the registration is complete, you will be redirected to a ‘registration complete’ page, where you can access your API key.
Now that we have our API key, let’s start writing some code in Python. We start by importing the ‘newsapi’ library.
from newsapi import NewsApiClient
Now, using the API key we got after registration, we create an object of the API.
newsapi = NewsApiClient(api_key='YOUR_API_KEY')
NewsAPI object has 3 main functions:
- get_sources() function returns the news sources and blogs from which the news will be retrieved.
- get_top_headlines() provides live top and breaking headlines for a country, specific category in a country, single source, or multiple sources. You can also search with keywords. Articles are sorted by the earliest date published first.
- get_everything() searches through millions of articles from over 30,000 large and small news sources and blogs. This includes breaking news as well as lesser articles.
Let’s use these functions in our code
news_sources = newsapi.get_sources()
for source in news_sources['sources']:
print(source['name'])
top_headlines = newsapi.get_top_headlines(
q='World War',
language='en',
)
for article in top_headlines['articles']:
print('Title : ',article['title'])
print('Description : ',article['description'],'\n\n')
all_articles = newsapi.get_everything(
q='World War',
language='en',
)
for article in all_articles['articles']:
print('Source : ',article['source']['name'])
print('Title : ',article['title'])
print('Description : ',article['description'],'\n\n')
In both get_top_headlines() and get_everything(), the query(q) passed is ‘World War’. You can send multiple keywords and can also implement logical operators for a combination of keywords.
The language parameter for the above functions is set to ‘en’ — English. The 2-letter ISO-639–1 code of the language you want to get headlines for. Possible options: ar, de, en, es, fr, he, it, nl, no, pt, ru, se, ud, zh. Default: all languages returned.
If you wish to fetch news from a particular source then you can provide that using the source parameter.
This was a really simple tutorial on how to fetch news using Python. I hope this article was helpful, do leave some claps if you liked it.
Open to suggestions, do not hesitate to give your valuable feedback.