Google News Search Python API Example

In this post, you will learn about how to use GoogleNews search Python library to get or retrieve or scrape news from Google News for last N number of days. This would be very helpful for someone wanting to track new work / projects in relation to machine learning, data science, deep learning or any field including sports, politics etc. Without further ado, lets jump in right away. You can log into Google colab and practise the code. 

Step 1: First and foremost, lets install GoogleNews python library.

pip install GoogleNews
PIP Install GoogleNews Python Library
Fig 1. PIP Install GoogleNews Python Library

Step 2: Instantiate GoogleNews object. One can pass the language and period to instantiate the object. The parameter, period, represents the news from the last N number of days. One can represent the last 3 days as ‘3d’ or yesterday as ‘1d’

from GoogleNews import GoogleNews
'''
Language: lang as English 
Period: period as number, N, representing news from last N days
'''
googlenews = GoogleNews(lang='en', period='1d')

Step 3: Get the news of the last few days based on the search text. Method, search, is used to get the result as a list of JSON objects. For storing results, the result method is invoked on the GoogleNews object. The parameter sort is passed TRUE to sort the result with the first result as most latest one. Note clear method which is used to clear GoogleNews object such that fresh search results get stored in the results object.

Search news from GoogleNews object search method
Fig 2. Search news from GoogleNews object search method

Step 4: Iterate through result objects to print title, description, and URL for each news.

'''
Print the title, description and URL of the news
'''
for result in results:
  print('\n\nTITLE:', result['title'], '\nDESC:', result['desc'], '\nURL: ', result['link'])
Fig 3. Iterate through GoogleNews search results
Ajitesh Kumar
Follow me
Latest posts by Ajitesh Kumar (see all)

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking. Check out my other blog, Revive-n-Thrive.com
Posted in Python. Tagged with .

Leave a Reply

Your email address will not be published. Required fields are marked *