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
- Make sure you have python installed
- Get an API Key
- Subscribe to the Google News API
- Use the Google News API with Python
- 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.
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 hello.py 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”:
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("google-news.p.rapidapi.com")headers = { 'x-rapidapi-host': "google-news.p.rapidapi.com", 'x-rapidapi-key': "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" }conn.request("GET", "/v1/search?country=US&lang=en&q=Elon%20Musk", headers=headers)res = conn.getresponse()data = res.read()print(data.decode("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 https://realpython.com/python-pathlib/data_folder = Path("C:/Users/myUserName/Documents/")outputFile = data_folder / "newsTrends.csv"# datetime object containing current date and timenow = datetime.now()# 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("google-news.p.rapidapi.com")headers = { 'x-rapidapi-host': "google-news.p.rapidapi.com", 'x-rapidapi-key': "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" }conn.request("GET", "/v1/search?when=1h&country=US&lang=en&q="+query, headers=headers)res = conn.getresponse()data = res.read()# 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:
"C:\path\to\python.exe" "C:\path\to\captureNewsTrends.py"
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:
There are a few interesting things to note from this graph:
- It looks like Trump got slightly more exposure than Biden. But it still looks very close between those 2 candidates.
- 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.
- 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.
Conclusion
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:
- 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.
- There are many other search queries which people might use that would expose them to political messages. We used one query.
- 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.
- 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:
The winner needs to receive 270 electoral votes so it is not over yet.
5/5 - (1 vote)
FAQs
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.
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? ›- Select or create a Cloud Platform project.
- Enable billing for your project.
- Enable the API Keys.
- Setup Authentication.
- Top headlines /v2/top-headlines.
- Create a URL.
- Create the request to GET news headlines.
- Test the connection if it returns 200 means connection is ready.
- Convert headlines to JSON format.
- mediastack. mediastack. ...
- Bing News Search API. Microsoft. ...
- AYLIEN. AYLIEN. ...
- Usearch. Usearch. ...
- Webz.io. Webz.io. ...
- NewsCatcher. NewsCatcher. ...
- Connexun. connexun. ...
- The New York Times API. The New York Times.
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? ›
- Get an API key. An API Key is (usually) a unique string of letters and numbers. ...
- Test API Endpoints with Python. ...
- Make your first Python app with API.
Google API Client Library for Python
An older version of client libraries for easily accessing REST interfaces of APIs.
- 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.
- Access the HTML of the webpage and extract useful information/data from it.
- def get_data(self, api):
- response = requests.get(f"{api}")
- if response.status_code == 200:
- print("sucessfully fetched the data")
- self.formatted_print(response.json())
- else:
- print(f"Hello person, there's a {response.status_code} error with your request")
- 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.
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.