Pytrends is the unofficial API for Google Trends that allows you to automate the process of downloading different reports from Google Trends and can be used within Python to collect data using the different methods available within the API.
Google Trends is a free resource created by Google that analyses and lists the trending search terms over a period of time, across different regions and timezones, along with suggestions for keywords and topics based on a given term.
I have been playing around with this for a couple of weeks and wanted to share some of the interesting ways it can be used for SEO purposes. This first post will be an introduction to the API and the methods available, followed by the second part which will cover how to visualise the data.
Before starting we need to install the required packages and libraries and connect to Google. I have also added some additional libraries to help store and visualise the data.
Now we have all the necessary packages installed, we can start coding! Next we need build the payload which will allow us to enter parameters such as our keyword list, the timeframe to collect data, a region and timezone, as well as narrowing down further to a specific category.
Parameters to add
There are several parameters you can add to your payload, however the only required one is the keyword list where you can add the keywords you want to get data for. You can add up to 5 terms in a list, which is created like this:
Other parameters include:
timeframe- While this isn’t mandatory, as it will default to the last 5 years, you can set a timeframe you want to collect data from. You can do this in a couple of different ways:
- With specific dates set in a YYYY-MM-DD YYYY-MM-DD format. E.g. 2020-02-27 2020-03-27
- Current time minus the number of months using the format ‘today #-m’ where # is the number of months from the date you want to collect data from. E.g. ‘today 2-m’ will collect data from today to 2 months ago.
- Current time minus the number of days using the format ‘now #-d’ where # is the number of days from the date you want to collect data from. E.g. ‘now 7-d’ to collect data from the last 7 days. Note this seems to only work for 1-7 days.
cat- Allows you to use a category to narrow down results, this uses the integer for the category, which you can find here. This isn’t compulsory, as it will default to no category.
geo- Allows you to narrow down to a specific country, using the two letter country abbreviation, eg ‘GB’. Again this isn’t compulsory, as it will default to ‘World’.
gprop- The Google property you want to filter to (images, news, YouTube, Google Shopping). This will default to web search.
So say for example I want to collect data over the last year for the popularity of searches for my 3 favourite k-pop groups, using the music category (35), in the US, I would build my payload as follows:
Now for the fun bit, collecting different types of data. There are several API methods you can use with the Pytrends library, which you can find more about here but I’m just going to share a few which may be most useful for SEO use cases.
Interest over time
This will return historical data for when a keyword was searched for the most, over the timeframe you select. This is reflective of what is shown in the 'interest over time' section of Google Trends.
For this example I have created a keyword list of five dog breeds to see how the search volume for these terms has changed over the last 3 months. I also turned the results given into a Pandas DataFrame in order to analyse the data easily.
This will generate a DataFrame with a column for each date, and each search term in my keyword list, with the search volume for each day.
Interest by region
Using this method we will be able to see data for where a keyword is most searched for. There are a couple of different parameters you can add to this:
resolution- Can be set to CITY, COUNTRY or REGION.
inc_low_vol- Either a True or False boolean to determine whether to display data for low volume countries.
inc_geo_code- This can also be set to either True or False, depending on whether you want to include ISO codes along with the country names.
I have two examples of this method, the first with no parameters, which displays all countries and the interest of the search term across these.
The other displays region level data for the country Geo code 'GB'.
This will return data for keywords related to the provided keyword, and the output is displayed in two parts:
- Top: Which displays a Google Trends score between 0-100.
- Rising: Displays how much interest the keyword has increased by in a %.
For this example, my keyword list contains 5 endangered animals from the WWF site, where I collect the values for all of the terms. I will then use the
.get method to display the rising and top values for a selected keyword.
The suggestions method will display a list of additional suggested keywords that can also be used for a trend search.
In my example I have defined a keyword within the method and displayed the results as a dataframe.
The final method I want to share is the trending searches. This will return data for the latest trending searches, either worldwide or for the country you include within the method.
I also shared an example of a Google Data Studio dashboard I have created using the trending searches method for both the UK and US, which can be updated daily.
Just because I'm a bit of a nerd and wanted to try out the Google Trends API in Python I now have a dashboard displaying the top 20 trending searches in the UK and US every day. It literally serves no purpose except for some occasional lols 🤷♀️ pic.twitter.com/CUB0d8xgsG— Ruth⁷ (@rvtheverett) July 20, 2020
I hope this has provided a good introduction to the Pytrends API and how it can be used in Python to find some interesting data for different search terms. The most powerful part of this though is the visulisations we can create from the data. The concluding part of this post will share how I use the Python libraries MatPlotLib and Seaborn to create visulations, as well as how to connect the data to Google Sheets in order to display results within a Google DataStudio dashboard.
You can find a Google Colab notebook here, with the examples shared above if you would like to save and use as a starting point.
If you have any questions, or suggestions, feel free to get in touch with me on Twitter @rvtheverett