Get Google Trends Results Using Python
Sajid Hasan Sifat
Data Consultant | Business Intelligence Consultant | Sr. Data Analyst Yassir | BI Analyst VML | Ex-Sr BI Analyst at 10 Minute School | Ex- Robi Axiata Ltd | Ex Data Analyst - Daraz ( Alibaba Group )
How to get rising related queries and top related queries from Google Trends by Pytrends (the unofficial API for Google Trends)
Google Trends can give us some very insightful data. The queries that google trends returns can be used for digital marketing, SEO, and lots more. Here’s how we can get the queries from google trends using Python.
Libraries required:
import pandas as pd
from pytrends.request import TrendReq
If you have not installed the libraries and if you are on windows, you can use pip to install the libraries. Just go the terminal and write these commands to install them:
>pip install pandas
>pip install pytrends
You can read the documentation here.
Go to any Python editor or environment and start coding. Here, we have created a payload for the API. We want to search the related queries for “data science”.
pytrend = TrendReq(hl=’en-US’, tz=360)
The arguments hl=’en-US’ is for the language you want to search. The tz=360 is for timezone. Here, US CST is “360” (note: not -360. Google uses timezone like this):
# Create payload and capture API tokens. Only needed for interest_over_time(), interest_by_region() & related_queries()trendy_keywords = ‘Data Science’
kw_list = [trendy_keywords]
kw = trendy_keywords
pytrend.build_payload(kw_list)
We kept the keyword in a list and in a string.
pytrend.build_payload(kw_list) builds the payload and accepts a list as an argument. You can also build the payload for interest over time and interest by region like this:
pytrend.interest_over_time()
pytrend.interest_by_region()
We want to find out the related queries:
领英推荐
# Related Queries, returns a dictionary of dataframesrelated_queries_dict = pytrend.related_queries()
print(related_queries_dict)
pytrend.related_queries() returns a dictionary, if we print we get an output like this:
This is not quite organized. So, we can split the data for top and rising into different dictionaries.
# for rising related queries
related_queries_rising = related_queries_dict.get(kw).get(‘rising’)# for top related queries
related_queries_top = related_queries_dict.get(kw).get(‘top’)print(“**************** RISING RELATED KEYWORDS **************”)
print(related_queries_rising)print(“**************** TOP RELATED KEYWORDS *******************”)
print(related_queries_top)
Now we have data in different dictionaries.
These keywords can be further used in the Google Ads campaign to find the top and rising keywords to bid in Google ads. Every month the queries can be checked to find the best keywords. This is an easy way to get a lot of keywords.
*****************************Full Code******************************
# Import necessary libraries
import pandas as pd
from pytrends.request import TrendReq
# Set up the payload for the Google Trends API
pytrend = TrendReq(hl='en-US', tz=360)
# Specify the keyword for which you want to find related queries
trendy_keywords = 'Data Science'
kw_list = [trendy_keywords]
kw = trendy_keywords
pytrend.build_payload(kw_list)
# Get related queries (both rising and top)
related_queries_dict = pytrend.related_queries()
# Split the data into rising and top related queries dictionaries
related_queries_rising = related_queries_dict.get(kw).get('rising')
related_queries_top = related_queries_dict.get(kw).get('top')
# Print the results
print("**************** RISING RELATED KEYWORDS **************")
print(related_queries_rising)
print("**************** TOP RELATED KEYWORDS *******************")
print(related_queries_top)
Conclusion
There’s a lot that can be done through this Python library. You can visit their documentation to come up with more exciting projects.