Get Google Trends Results Using Python
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.
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)
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
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:
We want to find out the related queries:
# Related Queries, returns a dictionary of dataframesrelated_queries_dict = pytrend.related_queries()
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 *******************”)
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.
There’s a lot that can be done through this Python library. You can visit their documentation to come up with more exciting projects.