How to use the Google News API with Python - Rapid Blog (2023)

Table of Contents

  • A Five-Step Process
    • Step 1. Make sure you have python installed
    • Step 2. Get an API Key
    • Step 3. Subscribe to the Google News API
    • Step 4. Use the Google News API with Python
    • Step 5. Chart the Results
  • Conclusion

Google News is a service that we can use to take a pulse of a popular topic. Currently, there is a presidential election happening in the United States. With this event, we have an opportunity for news data analysis. In the realm of marketing, there is a concept of Effective Frequency. This refers to how many times you need to expose people to a message or idea before making a buy decision. In an election, the buy is a vote, and the message is which candidate to vote for. In this article, we will walk through how to use the Google News API with Python. This will allow us to capture data over time and analyze it.

The idea we will test is about media exposure for presidential candidates. Who will have their name appear more often in the news for a few days leading up to the election? Yes, that does not guarantee exposure to the winning candidate more often. News articles are not the only source of media. And it does not ensure that the names are being used in a positive light. But it should give us some signs of the public awareness of the candidates.

The following list is a summary of the process we will follow:

A Five-Step Process

  1. Make sure you have python installed
  2. Get an API Key
  3. Subscribe to the Google News API
  4. Use the Google News API with Python
  5. Chart the results

Step 1. Make sure you have python installed

For this article, we will be using a computer running Windows to run the Python code. I installed Python version 3 from the Python installation instructions for Windows.

(Video) Scraping Google News the Easy Way with Python and pygooglenews

But to be sure, we will put up this sample code and try running it:

#!/usr/bin/env pythonimport syssys.stdout.write("hello from Python %sn" % (sys.version,))

When I save the code as and run this on my machine I see this:

>python hello.pyhello from Python 3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)]

Python version 3.x is required to use the http.client library in the sample Python code for the Google News API.

Step 2. Get an API Key

Once we know Python is available, we need to get an API Key. The Google News API we will be using is hosted on the RapidAPI platform. Getting a key is a simple process that is free. Go to the RapidAPI home page and use an email address or social media account to connect.

Step 3. Subscribe to the Google News API

Once you register on the RapidAPI platform, the next step is to subscribe to the Google News API. You can do that by clicking on the blue button on the endpoints page which says “Subscribe to Test”:

How to use the Google News API with Python - Rapid Blog (1)

(Video) News API - Python Code

After subscribing, I use Search endpoint. I would use the “Topic Headlines” endpoint but I need a custom topic. I want to search for US Presidential Election so I use the Search endpoint to do so.

Step 4. Use the Google News API with Python

Now that we have made sure Python is available and subscribed to an API, it’s time to use it. First we will start with the sample code on the Endpoints page for Python http.client. This uses built in Python libraries (when you have Python version 3+ installed).

import http.clientconn = http.client.HTTPSConnection("")headers = { 'x-rapidapi-host': "", 'x-rapidapi-key': "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" }conn.request("GET", "/v1/search?country=US&lang=en&q=Elon%20Musk", headers=headers)res = conn.getresponse()data ="utf-8"))

You’ll need to replace the rapidapi-key Xs with your actual key. You will see this on the endpoints page when you are logged in.

The sample code pulls posts in JSON format. Next, we will use Python to populate a local summary text file with the results:

#! python"""File: captureNewsTrends.pyDescription: This script pulls a list of items from the Google News API. Then it loops through results to tally the frequency of key words in the title of each news article. The counts for a given date and time are saved into a file."""#import libraries used belowimport http.clientimport jsonfrom datetime import datetimefrom pathlib import Path# This is where the generated html will be saved (in the local directory)#More information about the Path function is described at = Path("C:/Users/myUserName/Documents/")outputFile = data_folder / "newsTrends.csv"# datetime object containing current date and timenow = Get the date and time in the format YYYY-mm-dd H:M:Sdt_string = now.strftime("%Y-%m-%d %H:%M:%S")# Initialize counterstrumpCnt=0bidenCnt=0jorgensenCnt=0articleCnt=0# Set to 1 to show details along the way for debugging purposesdebug=0#This is the url encoded query we will search for in the Google News API#Spaces are replaced with %20query = "US%20Presidential%20Election"#Connect to the APIconn = http.client.HTTPSConnection("")headers = { 'x-rapidapi-host': "", 'x-rapidapi-key': "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" }conn.request("GET", "/v1/search?when=1h&country=US&lang=en&q="+query, headers=headers)res = conn.getresponse()data = Load API response into a Python dictionary object, encoded as utf-8 stringjson_dictionary = json.loads(data.decode("utf-8"))# Loop through dictionary keys to access each articlefor item in json_dictionary['articles']: # Pull the title for this article into a variable. thisTitle = item['title'] if debug>0: print("Title:", thisTitle) #Get a count of keywords in the article title trumpCnt+= thisTitle.upper().count("TRUMP") bidenCnt+= thisTitle.upper().count("BIDEN") jorgensenCnt+= thisTitle.upper().count("JORGENSEN") articleCnt+=1# Create summary line for the csv fileoutputCSV = str(dt_string) + "," + str(articleCnt) + "," +str(trumpCnt) + "," + str(bidenCnt) + "," + str(jorgensenCnt) + "\n"#Now populate the csv filewith open(outputFile, "a", encoding="utf-8") as f: f.write(outputCSV)

Again, to use the code above you’ll need to replace the Xs with your RapidAPI Key. Also replace the path with a local path on your computer or server. What the code will do is to pull a list of news articles posted within the last hour. The source is Google News and the query is US Presidential Election. Then it loops through each article title retrieved. For each article it checks for the last name of the current presidential candidates. Finally it adds a row to a local Comma Separated Value (CSV) file.

To get a data point every hour, I create a batch file called captureNewsTrends.bat like this:

(Video) Display latest news using python

"C:\path\to\python.exe" "C:\path\to\"

Then using the windows scheduler I set the batch file to run once per hour to populate the csv file. The query used in this example has under 60 results for each of the API calls. This is for every hour in several days leading up to the election. If you expand the search topic or the time requested you will get more results. But this API call only returns the first 100 results. That’s why it’s important to limit the results and/or the time frame to get a smaller number of them at a time.

Step 5. Chart the Results

Once the election is over, we can stop capturing information every hour. At that point we have a csv file which we can chart on a graph. Using Excel the trend graph obtained from this script looks like this:

How to use the Google News API with Python - Rapid Blog (2)

There are a few interesting things to note from this graph:

  1. It looks like Trump got slightly more exposure than Biden. But it still looks very close between those 2 candidates.
  2. Jorgensen did not appear in any of the news headlines. That indicates that the “2 party system” is still dominant. This matches the election results. None of the states are registering more than 2% of the votes to anyone besides Trump or Biden.
  3. As of this writing (2 days after the election) votes are still being counted and there is no winner yet. Even the states that have almost all their votes counted are very close between the 2 top candidates. That closeness matches the chart.


In this article, we have walked through an example of using the Google News API with Python. We started by getting set up with the API and then used Python to create a CSV file with the results. Then we charted the results in a graph using Excel.

The thesis under test is whether media exposure impacts election results. This idea comes from an old marketing axiom called the “Rule of 7”. This refers to how often people need to be exposed to a message before making a purchase. In this experiment the following limitations apply:

(Video) Blogger API with python fully and easily

  1. There are many ways to be exposed to messages that were not considered in the data analyzed. For example political signs have littered roadsides all over the country for several weeks if not months. Most of them have the last name of candidates prominent on the sign. Many have just that, though for lesser-known candidates first names are often included.
  2. There are many other search queries which people might use that would expose them to political messages. We used one query.
  3. The tone or emphasis of each article was not captured. Much of the current political news articles highlight negative aspects of candidates. The positivity of each message was not measured. This might be a good use for the Sentiment Analysis API.
  4. Correlation does not imply causality. Just because the exposure may correlate with vote count, it does not mean that is the cause.

With that aside, there is a strong correlation between exposure and votes. Biden and Trump received vs what percentage of people voted for anyone else. Personally I only remember seeing 3 candidates on the ballot, but there are others in the race:

How to use the Google News API with Python - Rapid Blog (3)

The winner needs to receive 270 electoral votes so it is not over yet.

(Video) How To Read Latest News Using News API in Python - Get the Daily News Headlines with HTTP REST API

5/5 - (1 vote)


Is Google News API deprecated? ›

FAQ For Google News API

Google has an API for Google News. However, the tech giant has deprecated it. As a result, the API is no longer maintained. But it is still available for public access.

Can I still use Google News API? ›

Google has deprecated the Google News Search API and no longer maintains it, but the API is still working and is available for public access. The API allows you to integrate Google News search results into your application or web pages.

How do I use Google API key in Python? ›

Quick Start
  1. Select or create a Cloud Platform project.
  2. Enable billing for your project.
  3. Enable the API Keys.
  4. Setup Authentication.
Jul 6, 2023

How to use news API in Python? ›

Lets start with top news headlines.
  1. Top headlines /v2/top-headlines.
  2. Create a URL.
  3. Create the request to GET news headlines.
  4. Test the connection if it returns 200 means connection is ready.
  5. Convert headlines to JSON format.

Which news API is best? ›

Compare the Top News APIs of 2023
  • mediastack. mediastack. ...
  • Bing News Search API. Microsoft. ...
  • Usearch. Usearch. ...
  • ...
  • NewsCatcher. NewsCatcher. ...
  • Connexun. connexun. ...
  • The New York Times API. The New York Times.

What is the alternative to Google News API? ›

Bing News API delivers the data in JSON format Which is a quick response to your query. Overall, it's one of the best Google News API alternatives for 2023. Moreover, the features of Bing news API are: Possibility to access news data through webhooks.

What is the most used Google API? ›

1. Google Sheets API. The Google Sheets API is perhaps the most used Google API there is.

What is better than Google News? ›

The best alternatives to Google News are Google, Newsadoo, and Ausum. If these 3 options don't work for you, we've listed over 20 alternatives below. What do you think of Google News?

Can you use Google API without paying? ›

All Maps Embed API requests are available at no charge with unlimited usage.

Can you use Google API with Python? ›

Install the Google API Client Library for Python

Get a Python representation of the AI Platform Prediction services. Use that representation to create a model in your project, which should help you understand how to call the other model and job management APIs.

How to run API in Python? ›

How to Start Using an API with Python
  1. Get an API key. An API Key is (usually) a unique string of letters and numbers. ...
  2. Test API Endpoints with Python. ...
  3. Make your first Python app with API.

Does Google have a Python API? ›

Google API Client Library for Python

An older version of client libraries for easily accessing REST interfaces of APIs.

How to extract news from website using Python? ›

Newspaper scraping using Python and News API
  1. Use the API of the website (if it exists). For example, Facebook has the Facebook Graph API which allows retrieval of data posted on Facebook.
  2. Access the HTML of the webpage and extract useful information/data from it.
Dec 8, 2022

How to use API to fetch data in Python? ›

Make your API call
  1. def get_data(self, api):
  2. response = requests.get(f"{api}")
  3. if response.status_code == 200:
  4. print("sucessfully fetched the data")
  5. self.formatted_print(response.json())
  6. else:
  7. print(f"Hello person, there's a {response.status_code} error with your request")

What is the alternative to Google News 2023? ›

Top world & local news stories
  • Google. 875 reviews. 750 alternatives. ...
  • Newsadoo. 6 reviews. 7 alternatives. ...
  • Ausum. 6 reviews. 8 alternatives. ...
  • Draft. 12 reviews. 9 alternatives. ...
  • Not Depressing News. 16 reviews. 11 alternatives. ...
  • Volv. 40 reviews. 18 alternatives. ...
  • Below the Fold. 12 reviews. 7 alternatives. ...
  • Microsoft Hummingbird. 7 alternatives.

Is Google Earth API discontinued? ›

Google Earth Plugin API has been deprecated as of December 12th, 2014. The API will continue to work on supported browsers until December 12th, 2015, and will shut down on that date. What alternative exist?

Why did Google change Google News? ›

Our new look for Google News on desktop was inspired by feedback we received from readers. We've made it easier for you to catch up on the most important news by bringing Top stories, Local news and personalized picks for you to the top of the page.

Why isn t Google News changing? ›

App doesn't refresh

To get new stories, open your app and swipe down on the screen. Check that you're connected to Wi-Fi or mobile data. If the app closes or specific content is blank, learn how to clear your app's cache and data.


1. How to Use Google Indexing API For Beginners | Index 2,00,000 Pages in 5 Minutes
(Amit Tiwari)
2. Google Indexing API for Blogger with Proof | How to Index Blog on Google
(Blogging Raptor)
3. Guaranteed Google Article Indexing: Unlock Rapid Results
(Ismail Blogger)
4. How to use a real API in your web app (rapidAPI tutorial)
(Double D)
5. How To Use ChatGPT To Fully Automate Web Scraping
6. How To Index Blog Post in Google Fast [In Just 1 Hour] Via API Method🔥🔥
Top Articles
Latest Posts
Article information

Author: Nathanael Baumbach

Last Updated: 12/09/2023

Views: 5775

Rating: 4.4 / 5 (75 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Nathanael Baumbach

Birthday: 1998-12-02

Address: Apt. 829 751 Glover View, West Orlando, IN 22436

Phone: +901025288581

Job: Internal IT Coordinator

Hobby: Gunsmithing, Motor sports, Flying, Skiing, Hooping, Lego building, Ice skating

Introduction: My name is Nathanael Baumbach, I am a fantastic, nice, victorious, brave, healthy, cute, glorious person who loves writing and wants to share my knowledge and understanding with you.