Extracting Google Trends using Python

Filed Under: Python Modules
GoogleTrend Python FeaImg

Hey there coder! Today, We’ll look at a python program that will use the Pytrends module to successfully get data from Google Trends. Google Trends is a public platform that you can use to study interest over time for a certain topic, search phrase, and even company.

Let us first know what all data can one extracted through google trends using Python. You can extract the following data related to a given keyword you supply to the API:

  1. Interest Over Time
  2. Historical Hourly Interest
  3. Interest by Region
  4. Related Topics
  5. Related Queries
  6. Trending Searches
  7. Top Charts
  8. Keyword Suggestions

Code Implementation to get Google Trends Data

We will start off by importing the pytrends python package into the program along with the seaborn library to obtain the visualization of the trends. Pytrends is a Google Trends API that provides several methods to retrieve reports of trending results from google trends.

from pytrends.request import TrendReq
import seaborn

To extract the data, we first have to initialize a TrendReq object which will have a few attributes out of which we will be setting the language in the hl attribute along with the timezone in the tz attribute.

Request_object = TrendReq(hl="en-US", tz=360)

In order to extract the data, we will be using the build_payload function which as a number of attributes. The first attribute is a list of all the keywords and the second attribute is the timestamp which is set as ‘all’. We will try to compare some of the many programming languages available in the coding world.

Request_object.build_payload(["Python","C++","Java","R","JavaScript"], timeframe="all")

To extract the final data, we will be using the interest_over_time function which will return a dataframe that contains the keyword trends according to the date in the dataframe. We will be displaying the first 5 rows of the dataset.

lang_data = Request_object.interest_over_time()
GoogleTrends ExtractedDataFrame
GoogleTrends ExtractedDataFrame

Let’s plot the relative search difference between various programming languages over a certain time period:

lang_data.plot(figsize=(15,11),title="Trends of various Prog. Languages")
GoogleTrends Plot
GoogleTrends Plot

We can also extract data according to the region rather than over time using the interest_by_region function which has a few attributes as well. The code below does the job for us.

lang_data_region = Request_object.interest_by_region()

#excluding all 0 rows
lang_data_region = lang_data_region[(lang_data_region != 0).all(1)]
# dropping the nan values
lang_data_region.dropna(how='all',axis=0, inplace=True)

lang_data_region.plot(figsize=(30,11),title="Trends of various Prog. Languages Country-wise",kind ='bar')
GoogleTrends Plot Countrywise
GoogleTrends Plot Countrywise


Congratulations! You now know how to conveniently extract Google Trends data using Python and with the help of the pytrends library. You can also compare various cartoons or TV shows together.

Hope you liked the tutorial! Thank you for reading!

If you liked the tutorial, check out the following tutorials as well:

  1. Wikipedia Scraping In Python – Easy Ways
  2. Scrape Google Search Results using Python BeautifulSoup
  3. How to scrape Amazon Product Information using Beautiful Soup

Generic selectors
Exact matches only
Search in title
Search in content