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:
- Interest Over Time
- Historical Hourly Interest
- Interest by Region
- Related Topics
- Related Queries
- Trending Searches
- Top Charts
- 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 seaborn.set_style("darkgrid")
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
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.
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() lang_data.head()
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")
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')
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:
- Wikipedia Scraping In Python – Easy Ways
- Scrape Google Search Results using Python BeautifulSoup
- How to scrape Amazon Product Information using Beautiful Soup