How to use the Google Trends API with Python

Pytrends is an unofficial Google Trends API that provides different ways to download trending results reports from google trends. The python package can help you automate the process of fetching data and getting the result within a short period of time. In this article, we will look at five ways that can help us fetch data from Google Trends in different ways. The first method of the API is Interest_over_time, this method will return the historical data of the searched keyword from Google Trend according to the time frame you specified in the build_payload method.

Davis David

Data Scientist | Artificial Intelligence Practitioner | Software developer | technical writer

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

table of contents:

  1. What is Pytrends?
  2. How to install Pytrends
  3. Contact Google
  4. Payload construction
  5. Attention over time
  6. Historical interest hourly
  7. Attention by region’
  8. Related Inquiries
  9. Keyword Suggestion

What is Pytrends?

Pytrends is an unofficial Google Trends API that provides different ways to download trending results reports from google trends. The python package can help you automate the process of fetching data and getting the result within a short period of time.

In this article, we are going to look at five ways from the 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 device.

NB: pytrends requires python 3.3+ and the following python packages, lxml and pandas.

Contact Google

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

# connect to google 

from pytrends.request import TrendReq

pytrends = TrendReq(hl='en-US', tz=360) 

TrendReq receives two important parameters.

  • hl The hosting language for accessing Google Trends means; In this example, we set English.
  • tz Mean timezone, in this example we are using the US timezone (represented in minutes), which is 360.

For more details about the time zone, please check the following link.

Payload construction

The build_payload method from Pytrends is used to generate a list of keywords you want to search for in Google Trends. You can also specify the time frame for data collection and the category to query 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') 

In this example, we will be looking for pointersmachine learningKeyword. You can also add more keywords to kw_list as many as you like.

Attention over time

The first API method of pytrends is Interest_over_time; This method will return the historical data of the searched keyword from Google Trend according to the time frame you specified in the build_payload method.

Then we can visualize the collected data using the Plotly library to get more insights 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 chart, you can see the keyword “machine learningMost searched March 2021 to July 2021

Historical interest hourly

If you are interested in the hourly keyword, you can use the get_historical_intereset() method to fetch the hourly data according to the time you 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 defined the keywords we want to search for, start time, end time, and keyword category.

Attention by region

Sometimes you may be interested to see the keyword performance of each area. The method Interest_by_region from pytrends can show you which countries are searching for the keyword you selected on a scale from 0 to 100, where 100 represents countries with the most searches and 0 represents countries with insufficient 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 data frame format using pandas.

You can also use pandas to fetch countries with points greater than 10.

# by region greater than 10 searches
by_region[by_region["machine learning"] > 10]

Related Inquiries

Pytrends can also help you find keywords closely related to a primary keyword of your choice and then return a list of related keywords displayed in Google Trends. Let’s find a list of queries related to Machine Learning and show the most important queries.

data  = pytrends.related_queries()

data['machine learning']['top'] 

As you can see, there are different keywords associated with “machine learning”, such as “what is machine learning,” And the “machine learning model.

Keyword Suggestion

Google Trends can provide you with a list of keyword suggestions related to your primary keyword. In the example below, you will submit a request to find suggestions for a keyword called “business intelligence”.

The pytrends suggestions method 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 data window above, 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 ways to cover than the Pytrends package; I recommend that you take your time to read the other methods in the documentation. Check it out here.

If you learned something new or enjoyed reading this article, please share it so others can see it. Until then, see you in the next post!

You can also find me on Twitter .

And you can read more articles like this here.

Want to keep up with what’s new in Python? Subscribe to our newsletter in the appendix below.

loading
. . . comments & more!

Leave a Reply

%d bloggers like this: