A Quick Start Into Google Trends Using pytrends

Google Trends is a website by Google that analyzes the popularity of top search queries in Google Search across various regions and languages. The website uses graphs to compare the search volume of different queries over time.

In this article, i’ll show you how to use the “Unofficial API for Google Trends” known as “Pytrends”.

It is good to know that: since —pytrends is a pseudo API for Google Trends, it may not work anymore if Google decides to change their backend in that specific module.

  • Python 2.7+ (I’m using 3.9.2).
  • pip (I’m using pip3).

Since we have Python and pip installed. We will need to install pandas, and pytrends, using the following codes:

  • pip install pandas or pip3 install pandas.
  • pip install pytrends or pip3 install pytrends.

Now, we need to import the pandas to store and visualize the data we are requesting from Google. After that, we will import the pytrends package and create an instance of the method called TrendReq(). This method — will store our session and allows us to communicate with Google Trends API. You just need to import it on the beginning of your code.

These parameters are optional. You can see all the originals parameters on the request.py original file.

After the import and instance process, we will make our first request. I created a list with just one word to search, that is “Bitcoin”. That list will be used on our pytrends.build_payload(). This method, captures the API tokens and will be necessary for other methods that we will see soon. So, it’s a good choice to have it in the beginning of the code too.

For the pytrends.build_payload() method we only use the list of words parameter and the timeframe that i set it on 3 months ago until today.

Then we finally call the pytrends.interest_over_time()method!

And when we print the result, we get…

Now we will call:pytrend.get_historical_interest. From this method, we will get the return of each hour, over the day(s) we passed as parameter. Check it out!

You can see that i choose today’s date (03/31/2021), and the afternoon period. Let’s see the return of it.

Here we had the pandas dataframe as return again, now based on hours.

Here another interesting one: pytrends.interest_by_region() . This method will give us the interest by city, country, DMA or region. Take a look.

Notice that, we need the pytrends.build_payload() here again. Then we get the following pandas dataframe.

This one is very interesting too. pytrends.related_queries() request data from Google’s Related Queries section and return a dictionary of dataframes. If no top and/or rising related queries are found, the value for the key “top” and/or “rising” will be None. This method doesn’t need any parameters.

For better visualization i just had to make a loop through the items of search term result. Then i got:

It’s not perfectly aligned but, you can see the top and rising queries with their values in the following column.

Here we havepytrends.trending_searches() that request data from Google’s Hot Searches section. The only one parameter is called “pn”, and the default value is “united_states”.

Then we get this dataframe:

Let’s see what people wanted to know about in the given year. The function pytrends.top_charts() request data from Google’s Top Charts section and return a dataframe.

Covid19 year =(. Then…

  • Pytrends allows us to get some good informations about what is being searched on Google. And it may give us valuable data.
  • I chose to include the methods most relevant to me. But you can check the whole project description here. Also the parameters and an overall better explanation.
  • Please, feel free to complement this article in the comments.

That’s it. This is my first article, so, forgive my mistakes. Thank you all!

Just me.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store