PyTrends – Google Trends API for Python

5/5 - (2 votes)

Keeping Up With Market Changes By Using Google Trends API

In this fast-moving world, people’s needs are constantly changing. To know about shifting behaviors, you can use Google Trends.  

This free tool provides data through search requests across Google Search, YouTube, Shopping, and Images.

By entering a keyword or a topic, you can explore what the world, your target market or audience is searching for.

The insight that you can get into these actual search terms can help develop content strategy as well as overall marketing strategy.

Searches During Covid-Stricken Periods

As a gauge of how search trends change:  From March 2020 to mid 2022 searches for “how to make hand sanitizer” grew over 4,000% worldwide. 

Meanwhile, searches for “can you get coronavirus twice” grew about 600%. 

And searches for “grocery delivery service near me” went up about 200% globally.

Pytrends, Google Trends API for Python 

You can use Google Trends as a public platform to analyze interest over time.

It is easy to use manually but when doing a large-scale project, which requires building a large dataset, it can get cumbersome.

In fact, researching and copying data by hand from the Google Trends site is not only time-consuming, it’s also boring. Your computer should do that work!

🤗 When using an API, time and effort are cut dramatically.

The unofficial Google Trends API for Python is PyTrends. It allows Python developers to quickly fetch search interest data that can be saved and used for more analysis later on.

Using Pytrends

Step 1. Install PyTrends Library

Install PyTrends in your Python shell with pip install pytrends or in your Jupyter notebook with !pip install pytrends.

!pip install pytrends

🌍 Recommended Tutorial: How to Install a Library in Python?

Step 2. Connect to Google Trends

Use TrendReq from pytrends.request library and import the pandas library to store and visualize the data. You can use the Trendreq parameters hl for host language, and tz for time-zone in minutes.

import pandas as pd  
from pytrends.request import TrendReq

#hl for host language, tz specifies timezone:
pytrend = TrendReq(hl='en-US', tz=360)  

Step 3. Set Interest By Region

Use the ‘build_payload’ method to specify your keyword(s) in a list

Here one keyword is used, but more can be added if needed. 

Google Trends return search interest 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. 

🛑 Attention: This is not the actual number of searches, but a share of total global searches by a region.

#build_payload for list of keyword(s)
pytrend.build_payload(kw_list=['python'])
df = pytrend.interest_by_region()  #for worldwide, parameter is left blank
df.head()

Step 4. Plotting Result on Bar Chart

‘China’ with its huge population stands out as the country with the highest proportion of searches.

df.reset_index().plot(x='geoName', y='python', figsize=(100, 8), kind ='bar') 

👑 Recommended Course: Matplotlib – A Complete Course [Finxter Academy]

Step 5. Get Google Keyword Suggestions

Returns a list of additional suggested keywords that can be used to refine a trend search.

keywords = pytrend.suggestions(keyword='python')
df = pd.DataFrame(keywords)
print(df)

Step 6. Daily Search Trends Worldwide, Using trending_searches()

Features trending or searches with increasing search volume.

# Get Google Hot Trends data
df = pytrend.trending_searches(pn='united_states')
print(df)

Step 7. Google Top Charts, by Year

Features searches with top volume for the period.

# Get Google Top Charts
df = pytrend.top_charts(2020, hl='en-US', tz=300, geo='GLOBAL')
print(df)

Step 8. Related Queries by Users

Returns data for the related keywords to a provided keyword in a dataframe dictionary, as shown in Google Trends’ Related Queries section.

#Related Queries for 'python', returns a dictionary of dataframe
pytrend.build_payload(kw_list=['python'])
related_queries = pytrend.related_queries()
related_queries.values()

These are just some examples of methods available.  For full documentation please refer to pytrends · PyPI.