Using Google Trends API With Python

In this tutorial article, we gonna learn to use Google Trends to getting Google’s popular search data.

Google Company offers search data & popular searches in Google Trends, and in this tutorial, we’ll use unofficial Google Trends API to access Google Trends in Python.

Introduction To Google Trends

Google Trends is a website that analyzes and lists popular search results in Google searches according to different regions and languages.

This API call in Python is pytrends. If you didn’t install it before, for install API follow the steps in the below section.

1 - Open your terminal
2 - Write pip install pytrends
3 - API is ready

Connecting To Google

You must connect to Google first because, after all, we are requesting the Google trending topics from Google Trends.

We need TrendReq method to connect to Google. You can find this method in pytrends.request library.

from pytrends.request import TrendReq

pt = TrendReq()

Getting Data From Google Trends

You can create many different datasets with Google Trends. In this section, we will learn how to obtain different types of data.

Daily Search Trends

You can use the trending_searches method to reach popular daily searches. This method has 1 parameter inside.

With this parameter, the country to receive daily searches data can be selected or daily popular calls around the world can be selected by default.

# Popular searches of France daily
france = pt.trending_searches("france")

# Popular searches of worldwide daily
world = pt.trending_searches()

# Show first 5 row
france.head()
Output:
                0
 0         Juventus
 1    Audrey Pulvar
 2    Suns – Lakers
 3     Lundi 24 mai
 4            Lorie
Google Keyword Suggestions
Google Keyword Suggestions

Next, let’s see how suggestions created by Google based on keywords can be obtained through Google Trends.

# For Creating Dataframe
import pandas as pd

# Get suggestion by PlayStation keyword
PS = pt.suggestions("PlayStation")

# Create Dataframe with keywords
PSDf = pd.DataFrame(PS)

# We don't need this column
PSDf = PSDf.drop(columns= "mid")

# Show first 5 row
PSDf.head()
Interest By Region

We can use the interest_by_region method to find out which countries around the world a particular search term is popular.

As an example, let’s find out in which country the term Xbox search is more popular.

# Genereate List by Xbox keyword
pt.build_payload(["Xbox"])

# Interest by Region
Xbox = pt.interest_by_region()

# Show first 10 row
print(Xbox.head(10))
 geoName              Xbox  
 Afghanistan           0
 Albania               0
 Algeria              41
 American Samoa        0
 Andorra               0
 Angola                0
 Anguilla              0
 Antarctica            0
 Antigua & Barbuda     0
 Argentina            17

The values are calculated on a scale from 0 to 100, where 100 is the location with the most popularity as a fraction of total searches in that location

A value of 50 indicates a location which is half as popular. A value of 0 indicates a location where there was not enough data for this term.

Top Charts (Trending List Of The Year)

Top charts method can be used to list the most popular search terms in the year you determined. Let’s use top charts to find out the best of 2020.

# Get Google Top Charts
top10 = pt.top_charts(2020,'en-US','GLOBAL')

# Delete exploreQuery column
top10 = top10.drop(columns = ["exploreQuery"])

# Show first 10 row
print(top10.head(10))
              title
 0           Coronavirus
 1      Election results
 2           Kobe Bryant
 3                  Zoom
 4                   IPL
 5  India vs New Zealand
 6    Coronavirus update
 7  Coronavirus symptoms
 8             Joe Biden
 9      Google Classroom

One thought on “Using Google Trends API With Python

  1. Hello there, You’ve done a great job. I will certainly digg it and personally recommend to my friends. I’m sure they’ll be benefited from this web site.|

Leave a Reply

Your email address will not be published. Required fields are marked *