Python

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
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.

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

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. 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.

Recent Posts

Agentic Reasoning Design Patterns in AI: Examples

In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…

1 month ago

LLMs for Adaptive Learning & Personalized Education

Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…

1 month ago

Sparse Mixture of Experts (MoE) Models: Examples

With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…

2 months ago

Anxiety Disorder Detection & Machine Learning Techniques

Anxiety is a common mental health condition that affects millions of people around the world.…

2 months ago

Confounder Features & Machine Learning Models: Examples

In machine learning, confounder features or variables can significantly affect the accuracy and validity of…

2 months ago

Credit Card Fraud Detection & Machine Learning

Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…

2 months ago