Google Trends API for Python. In this tutorial, I will demonstrate... | by Tanu N Prabhu | Towards Data Science

Google trends is a website that analyzes and lists the popular search results on Google search based on various regions and languages. Google Trends is Google’s website (obviously). With the help of this tutorial, you can get the trending results (and many more) from Google trends website using Python. You don’t need to manually search and copy the trending results, the Python API called pytrends does the job for you. Before getting started, I want all of you guys to go through the official documentation of the pytrends API.
pypi.org
Before getting started the whole code for this tutorial is available on my GitHub Repository which is given below. Please feel free to explore.
github.com
The first step is to install the library manually. So, open your favorite IDE or notebook start typing the following code. I will use Google Colab because it’s my favorite notebook.
If you are using jupyter notebook, just type the code as it is (make sure you have ‘!’ at the beginning)
!pip install pytrends
Or, if you are using an IDE, just type the following code
pip install pytrends
After executing the above code you should get a successful message as shown below:
You must connect to Google first because, after all, we are requesting the Google trending topics from Google Trends. For this, we need to import the method called TrendReq from pytrends.request library. Also, I will import the pandas library to store and visualize the data, which you see in the later tutorial.
import pandas as pd                        from pytrends.request import TrendReqpytrend = TrendReq()
Let us see the terms which are popular in the region worldwide. I will choose, the term to be searched as “Taylor Swift” (I like her so....).
pytrend.build_payload(kw_list=[‘Taylor Swift’])# Interest by Regiondf = pytrend.interest_by_region()df.head(10)
Now you might be thinking what are the values, what do they denote? 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. Source →Google Trends.
Let us plot the result on a bar graph because sometimes visual representation gives a clear picture.
df.reset_index().plot(x=’geoName’, y=’Taylor Swift’, figsize=(120, 10), kind =’bar’)
Also, you use the parameter resolution = 'COUNTRY_NAME' to filter the results.
Now let us get the top daily search trends worldwide. To do this we have to use the trending_searches() method. If you want to search worldwide just don’t pass any parameter.
# Get Google Hot Trends datadf = pytrend.trending_searches(pn=’united_states’)df.head()
Make sure you enter the country name in lowercase pn = "canada" . Also, you can compare the above results with the google trend’s result. To get today’s trending topics just use:
df = pytrend.today_searches(pn=’US’)
Let was see what was trending in 2019. With the help of top_charts method we can get the top trending searches yearly.
# Get Google Top Chartsdf = pytrend.top_charts(2019, hl='en-US', tz=300, geo='GLOBAL')df.head()
To compare the results just visit Google Trends. We can specify the year and the country that we want to see the trending searches.
Let us see how can we obtain google’s keyword suggestion. If you don’t know what I’m talking about. The below image explains things more clear.
# Get Google Keyword Suggestionskeywords = pytrend.suggestions(keyword='Mercedes Benz')df = pd.DataFrame(keywords)df.drop(columns= 'mid')   # This column makes no sense
It's a common thing that when a user searches for a topic, they would also search for something related. These are called related queries. Let us see what are the related queries for the topic “Coronavirus”. Always remember when you want to change the topic name just run the following code again with the new name as the parameter.
pytrend.build_payload(kw_list=['Coronavirus'])
Now let’s run the method related_queries which returns a dictionary full of related queries for the topic “Coronavirus”
# Related Queries, returns a dictionary of dataframesrelated_queries = pytrend.related_queries()related_queries.values()
Similarly, you can also search for the related topics just run the below code to do so:
# Related Topics, returns a dictionary of dataframesrelated_topic = pytrend.related_topics()related_topic.values()
This is the end of the tutorial, I hope you guys have learned a thing or two. If you guys have any doubts regarding the tutorial let me know via the comment section. Although this is a short tutorial there is a lot to learn. Alright see you in my next tutorial, have a good day!!!