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

Retrieval Augmented Generation (RAG) & LLM: Examples

Last updated: 25th Jan, 2025 Have you ever wondered how to seamlessly integrate the vast…

4 days ago

How to Setup MEAN App with LangChain.js

Hey there! As I venture into building agentic MEAN apps with LangChain.js, I wanted to…

1 week ago

Build AI Chatbots for SAAS Using LLMs, RAG, Multi-Agent Frameworks

Software-as-a-Service (SaaS) providers have long relied on traditional chatbot solutions like AWS Lex and Google…

2 weeks ago

Creating a RAG Application Using LangGraph: Example Code

Retrieval-Augmented Generation (RAG) is an innovative generative AI method that combines retrieval-based search with large…

3 weeks ago

Building a RAG Application with LangChain: Example Code

The combination of Retrieval-Augmented Generation (RAG) and powerful language models enables the development of sophisticated…

3 weeks ago

Building an OpenAI Chatbot with LangChain

Have you ever wondered how to use OpenAI APIs to create custom chatbots? With advancements…

3 weeks ago