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

What are AI Agents? How do they work?

Artificial Intelligence (AI) agents have started becoming an integral part of our lives. Imagine asking…

2 weeks ago

Agentic AI Design Patterns Examples

In the ever-evolving landscape of agentic AI workflows and applications, understanding and leveraging design patterns…

2 weeks ago

List of Agentic AI Resources, Papers, Courses

In this blog, I aim to provide a comprehensive list of valuable resources for learning…

2 weeks ago

Understanding FAR, FRR, and EER in Auth Systems

Have you ever wondered how systems determine whether to grant or deny access, and how…

3 weeks ago

Top 10 Gartner Technology Trends for 2025

What revolutionary technologies and industries will define the future of business in 2025? As we…

3 weeks ago

OpenAI GPT Models in 2024: What’s in it for Data Scientists

For data scientists and machine learning researchers, 2024 has been a landmark year in AI…

3 weeks ago