How to Use Google Trends API with Python

Written by davisdavid | Published 2021/10/07
Tech Story Tags: google | google-search | google-trends | python | blogging-fellowship | python-tutorials | google-trends-api-with-python | hackernoon-top-story

TLDR Pytrends is an unofficial Google Trends API that provides different methods to download reports of trending results from google trends. The python package can help you automate the process of fetching data and get the result over a short period of time. In this article, we will look at five methods that can help us fetch data from Google Trends in different ways. The first API method is interest_over_time, this method will return historical data of the searched keyword from Google Trend according to the timeframe you have specified in the build_payload method.via the TL;DR App

The Google Trends website provides an analysis of different search results from Google Search based on various criteria such as regions, time, and language. As a developer, you can use Google Trends API in python to get the same results as presented on the Google Trends website via Pytrends.

Table of Contents:

  1. What is Pytrends?
  2. How to Install Pytrends
  3. Connect to Google
  4. Build Payload
  5. Interest Over Time
  6. Historical Hourly Interest
  7. Interest by Region'
  8. Related Queries
  9. Keyword Suggestion

What is Pytrends?

Pytrends is an unofficial Google Trends API that provides different methods to download reports of trending results from google trends. The python package can help you automate the process of fetching data and get the result over a short period of time.
In this article, we will look at five methods from google trends API provided by Pytrends that can help us fetch data from Google Trends in different ways.
So, let's get started!

How to Install Pytrends

Run the following command to install Pytrends on your machine.
pip install pytrends
Note: pytrends requires python 3.3+ and the following python packages Requests, lxml, and pandas.

Connect to Google 

The first step after installation is to connect Pytrends to Google Trends so that you can send a request and get the information you need. You need to import TrendReq from pytrends to initialize the connection.
# connect to google 

from pytrends.request import TrendReq

pytrends = TrendReq(hl='en-US', tz=360) 
The TrendReq receives two important parameters.
  • hl stands for hosting language for accessing Google Trends; in this example, we set English.
  • tz stands for timezone, in this example, we use the US time zone (represented in minutes), which is 360.
For more details about the timezone, please check the following link.

Build Payload

The build_payload method from Pytrends is used to build a list of keywords you want to search in Google Trends. You can also specify the timeframe to gather data and the category to query the data from.
# build payload

kw_list = ["machine learning"] # list of keywords to get data 

pytrends.build_payload(kw_list, cat=0, timeframe='today 12-m') 
For this example, we will search trends for the “machine learning” keyword. You can also add more keywords into kw_list as many as you want.

Interest Over Time

The first API method from pytrends is interest_over_time; this method will return historical data of the searched keyword from Google Trend according to the timeframe you have specified in the build_payload method.
Then we can visualize the data collected by using the Plotly library to get more insight from the data.
#1 Interest over Time
data = pytrends.interest_over_time() 
data = data.reset_index() 


import plotly.express as px

fig = px.line(data, x="date", y=['machine learning'], title='Keyword Web Search Interest Over Time')
fig.show() 
From the above graph, you can see the keyword "machine learning" has been searched mostly from March 2021 to July 2021

Historical Hourly Interest

If you are interested in the hourly interest of the keyword, you can use the get_historical_intereset() method to fetch hourly data according to the time you have specified.
pytrends.get_historical_interest(kw_list, year_start=2021, month_start=9, day_start=1, hour_start=0, year_end=2021, month_end=9, day_end=30, hour_end=0, cat=0, sleep=0)
In the above code, we have specified the keywords we want to search, starting time, ending time, and the category of the keyword.

Interest by Region

Sometimes you can be interested to know the performance of the keyword per region. The method interest_by_region from pytrends can show you which countries search the keyword you selected on a scale of 0 to 100, where 100 represents a country with the most search and 0 represents a country that does not have enough data.
by_region = pytrends.interest_by_region(resolution='COUNTRY', inc_low_vol=True, inc_geo_code=False)

by_region.head(10) 
The results are returned in a data frame format by using pandas.
You can also use pandas to fetch countries that have a score of greater than 10.
# by region greater than 10 searches
by_region[by_region["machine learning"] > 10]

Related Queries

Pytrends can also help you find keywords that are closely tied to a primary keyword of your choice and then return a list of related keywords shown on Google Trends. Let us find a list of related queries for “machine learning” and return the top queries.
data  = pytrends.related_queries()

data['machine learning']['top'] 
As you can see, there are different keywords that are related to “machine learning,” such as "what is machine learning," and "machine learning model."

Keyword Suggestion

Google Trends can give you a list of keyword suggestions related to your primary keyword. In the example below, you will send a request to find suggestions for a keyword called "Business Intelligence."
 The suggestions method from pytrends will fetch keyword suggestions from Google Trends and return them in a data frame format.
keywords = pytrends.suggestions(keyword='Business Intelligence')
df = pd.DataFrame(keywords)
print(df)
In the above data frame, you can see a list of suggested keywords for ”Business Intelligence,” such as Business Intelligence Software, Market Intelligence, and others.

Reference

There are more API methods to cover from the Pytrends package; I recommend you take your time to read other methods in the documentation. Check it out here.
If you learned something new or enjoyed reading this article, please share it so that others can see it. Until then, see you in the next post!
You can also find me on Twitter @Davis_McDavid.
And you can read more articles like this here.
Want to keep up to date with all the latest in python? Subscribe to our newsletter in the footer below.

Written by davisdavid | Data Scientist | AI Practitioner | Software Developer| Technical Writer
Published by HackerNoon on 2021/10/07