Predictive Hacks

Get Google Trends using Python

google_trends

In this post, we will show how we can use Python to get data from Google Trends. Let’s have a look at the top trending searches for today in the US (14th of March, 2020). As we can see, the top search is about Coronavirus tips with more than 2M searches and at the 7th position is Rick Pitino with around 100K searches.

trending searches

Python package for getting the Google Trends

We will use the pytrends package which is an unofficial API for Google Trends which allows a simple interface for automating downloading of reports from Google Trends. The main feature is to allow the script to login to Google on your behalf to enable a higher rate limit. At this point, I want to mention that I couldn’t use this package and I created a new anaconda environment installing the pandas 0.25 version.

You can install the pytrends package with pip:

pip install pytrends

API Methods

The following API methods are available:

  • Interest Over Time: returns historical, indexed data for when the keyword was searched most as shown on Google Trends’ Interest Over Time section.
  • Historical Hourly Interest: returns historical, indexed, hourly data for when the keyword was searched most as shown on Google Trends’ Interest Over Time section. It sends multiple requests to Google, each retrieving one week of hourly data. It seems like this would be the only way to get historical, hourly data.
  • Interest by Region: returns data for where the keyword is most searched as shown on Google Trends’ Interest by Region section.
  • Related Topics: returns data for the related keywords to a provided keyword shown on Google Trends’ Related Topics section.
  • Related Queries: returns data for the related keywords to a provided keyword shown on Google Trends’ Related Queries section.
  • Trending Searches: returns data for latest trending searches shown on Google Trends’ Trending Searches section.
  • Top Charts: returns the data for a given topic shown in Google Trends’ Top Charts section.
  • Suggestions: returns a list of additional suggested keywords that can be used to refine a trend search.

Examples of Google Trends using Python

Below, we provide some walk-through examples. Let’s import the required packages.

import pandas as pd #pandas 0.25
from pytrends.request import TrendReq
pytrend = TrendReq()
 

Get the Hot/Trending Searches

By trending searches, we mean a new trend, meaning that people have started showing an interest in this keyword more than before in the last few days.


Let’s get the “hot searches” in the US.

trending_searches_df = pytrend.trending_searches(pn='united_states')
print(trending_searches_df.head(20))
                         0
0                       Pi
1   St. Patrick's Day 2020
2                 Frozen 2
3                 CNN live
4          UFC Fight Night
5           Conor McGregor
6      Domestic travel ban
7                    Spain
8              Trish Regan
9              Paracetamol
10               Westworld
11             Rick Pitino
12              Ronaldinho
13          Christian Wood
14               Bombshell
15            Vail Resorts
16             Matt Colvin
17             SNL tonight
18       Genesis P-Orridge
19            Shania Twain

Let’s get the “hot searches” in Greece and in Italy.

trending_searches_df = pytrend.trending_searches(pn='greece')
print(trending_searches_df.head(20))
 
                             0
0                        ΑΧΕΠΑ
1                   Χαρα βερρα
2                    Tzoker.gr
3                  Ραχηλ μακρη
4   ΕΛΛΑΔΑ κορωνοιοσ κρουσματα
5                       ΚΕΕΡΦΑ
6                        Αρκασ
7                  Σπυροπουλου
8                 Παρακεταμόλη
9                   Ξενοδοχεια
10                        1520
11   Στην υγειά μας, ρε παιδιά
12            Χριστίνα Βραχάλη
13      Κλεινουν τα ξενοδοχεία
14             Mohammed Khadda
15            Κορωνοιοσ Ελλάδα
16                 Φαιη σκορδα
17                    Κοτσιρασ
18            Just the 2 of us
19                      Twitch

trending_searches_df = pytrend.trending_searches(pn='italy')
print(trending_searches_df.head(20))
                                     0
0                       Buona Domenica
1                    Cristiano Ronaldo
2                    Vittorio Gregotti
3                               Buffon
4                              Zeppole
5                            Spiderman
6                    Genesis P-Orridge
7                           Ora legale
8          Santa Messa in diretta oggi
9                            Tom Hardy
10                        Diego Bianco
11   Autocertificazione spostamenti it
12                            Serenity
13                    Regione Sardegna
14  Soggetto di diritto internazionale
15                        Idi di marzo
16                               Totti
17           Dati Coronavirus 14 marzo
18                       Lara Zorzetto
19                           Cannavaro

Get the Google Daily Trends

Let’s see the “today searches” in the US.

# Get Google Hot Trends data
today_searches_df = pytrend.today_searches(pn='US')
print(today_searches_df.head(20))
 
0           Coronavirus tips
1                         Pi
2     St. Patrick's Day 2020
3                   Frozen 2
4                   CNN live
5            UFC Fight Night
6             Conor McGregor
7        Domestic travel ban
8                      Spain
9                Trish Regan
10               Paracetamol
11                 Westworld
12               Rick Pitino
13                Ronaldinho
14            Christian Wood
15                 Bombshell
16              Vail Resorts
17               Matt Colvin
18               SNL tonight
19         Genesis P-Orridge

Get Google Top Charts

Let’s have a look at the top charts in US for the 2019

# Get Google Top Charts
top_charts_df = pytrend.top_charts(2019, hl='en-US', tz=300, geo='US')
print(top_charts_df)
               title exploreQuery
0        Disney Plus             
1      Cameron Boyce             
2      Nipsey Hussle             
3   Hurricane Dorian             
4      Antonio Brown             
5         Luke Perry             
6  Avengers: Endgame             
7    Game of Thrones             
8          iPhone 11             
9    Jussie Smollett   

Get Google Keyword Suggestion

Let’s get a suggestion for the keyword “diet” and “vegan

# Get Google Keyword Suggestions
suggestions_dict = pytrend.suggestions(keyword='diet')
print(pd.DataFrame(suggestions_dict).drop('mid', axis=1))
 
                   title       type
0                   Diet  Nutrition
1                Dieting      Topic
2         Ketogenic diet      Topic
3     Mediterranean diet      Topic
4  Low-carbohydrate diet      Topic
suggestions_dict = pytrend.suggestions(keyword='vegan')
print(pd.DataFrame(suggestions_dict).drop('mid', axis=1))
 
           title     type
0       Veganism    Topic
1      Vegetable     Food
2         Veganz  Company
3   Vegan cheese    Topic
4  Veggie burger     Food

Get Interest Over Time

Let’s see the interest over time of the keyword “coronavirus

# Create payload and capture API tokens. Only needed for interest_over_time(), interest_by_region() & related_queries()
pytrend.build_payload(kw_list=['coronavirus'])

# Interest Over Time
interest_over_time_df = pytrend.interest_over_time()
print(interest_over_time_df.tail(10))
 
            coronavirus isPartial
date                             
2020-01-05            0     False
2020-01-12            0     False
2020-01-19            7     False
2020-01-26           21     False
2020-02-02           12     False
2020-02-09           10     False
2020-02-16            9     False
2020-02-23           38     False
2020-03-01           49     False
2020-03-08          100      True

As we can see in 2020-03-08 the keyword “coronavirus” reached the maximum score which is 100!

Interest By Region

Let’s have a look at the interest by Region for the keyword “coronavirus

# Interest by Region
interest_by_region_df = pytrend.interest_by_region()
print(interest_by_region_df.sort_values(['coronavirus'], ascending=False).head(20))
 
                      coronavirus
geoName                          
Italy                         100
Spain                          55
Ireland                        48
Switzerland                    45
France                         44
Singapore                      38
Austria                        35
United Kingdom                 35
Portugal                       34
Panama                         33
Germany                        33
Canada                         30
United States                  29
Australia                      27
Romania                        27
New Zealand                    25
United Arab Emirates           25
Belgium                        25
Peru                           22
Costa Rica                     22

Get the Related Queries

Let’s get the related queries of the keyword “coronavirus

# Related Queries, returns a dictionary of dataframes
related_queries_dict = pytrend.related_queries()
print(related_queries_dict['coronavirus']['top'].head(20))
print(related_queries_dict['coronavirus']['rising'].head(20))
 
                      query  value
0                    corona    100
1      coronavirus symptoms     97
2        coronavirus update     83
3          coronavirus news     76
4              corona virus     59
5         china coronavirus     54
6         coronavirus cases     49
7        italia coronavirus     49
8            coronavirus uk     45
9           coronavirus map     43
10       france coronavirus     36
11     sintomas coronavirus     30
12          coronavirus usa     29
13          usa coronavirus     29
14  symptoms of coronavirus     28
15           coronavirus us     28
16        coronavirus death     27
17        india coronavirus     26
18      what is coronavirus     25
19       coronavirus españa     24
                         query  value
0            wuhan coronavirus  83450
1                        wuhan  80850
2   ultime notizie coronavirus  57050
3                     covid 19  43950
4        coronavirus in italia  33650
5         us coronavirus cases  30250
6          coronavirus numbers  29600
7         coronavirus live map  28500
8            mappa coronavirus  26100
9            coronavirus morti  22300
10           spain coronavirus  22250
11     worldometer coronavirus  21350
12   coronavirus aggiornamenti  21100
13          coronavirus veneto  20800
14                   tom hanks  20700
15       tom hanks coronavirus  19950
16          coronavirus napoli  19900
17         coronavirus romania  19700
18        coronavirus piemonte  17650
19     coronavirus live update  17200

Get the Related Topics

Let’s get the related topics of the keyword “coronavirus

related_topic = pytrend.related_topics()
related_topic['coronavirus']['rising'].drop(['link','topic_mid'], axis=1).head(20)
 
	value	formattedValue	topic_title	topic_type
0	80500	Breakout	Wuhan	City in China
1	32750	Breakout	Cubit	Topic
2	31300	Breakout	Worldometers	Website
3	27550	Breakout	Última Hora	Topic
4	24150	Breakout	Covid	Electronics manufacturer in Tempe, Arizona
5	20200	Breakout	Veneto	Italian region
6	17650	Breakout	Decree	Topic
7	17100	Breakout	Piedmont	Italian region
8	15950	Breakout	Regions of Italy	Topic
9	15400	Breakout	Campania	Italian region
10	14550	Breakout	Tuscany	Italian region
11	14300	Breakout	Emilia-Romagna	Italian region
12	12850	Breakout	Apulia	Italian region
13	4800	+4,800%	Italian Language	Spoken language
14	3750	+3,750%	Italian people	Ethnic group
15	2200	+2,200%	Italy	Country in Europe
16	2100	+2,100%	China	Country in East Asia
17	1250	+1,250%	Spain	Country
Coursera Plus New Years promo banner that says

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Leave a Comment

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.

Python

Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s