How to track Google trends in Python using Pytrends
In this tutorial, we will learn how to track Google trends in Python using Pytrends.
Pytrends is the unofficial API for google trends in Python. This is a simple API that allows you to track the different trends going on in the world’s most popular search engine – Google. It logs in into google on your behalf and takes in data at a much higher rate than manually possible. However, this particular API will be functional only for the current Google backend technology. Once that is changed this API shall no longer hold good.
How to install Pytrends
For Python 2 installation :
pip install pytrends
For Python3 installation :
pip3 install pytrends
Connecting to Google
from pytrends.requests import Trendreq pytrends = TrendReq(hl='en-US', tz = 360)
What are Keywords?
Keywords are important words or phrases that help users find your content online. Whenever you type something in the search box Google looks out for certain terms – keywords – and then shows you all the pages where these keywords are present. Now for us to track Google trends, we need one or more keywords to search for. These could be anything from your favorite movie to academics to sports, politics, etc. And as we all know Google knows everything so it will give us the results very easily.
Search Keywords in Pytrends
Put in all the keywords we want to track in a list in Python. For this example, we are taking ‘Machine Learning’,’Python’ and ‘Linear regression’ all related to the subject in concern.
keyword_list = ['Machine Learning','Python','Linear Regression'] pytrends.build_payload(keyword_list, cat=0, timeframe='today 5-y', geo='', gprop='')
Different Filters over Searches
Interest By Region
# Interest by Region df = pytrends.interest_by_region(resolution='COUNTRY') print(df.head(10)) df = df.reset_index() df.plot(x="geoName", y="Machine Learning", figsize=(120, 10), kind ="bar")
The output of this code :
Trending searches :
To get in touch with all that is going on in today’s world, we use this method of trending searches.
#trending searches df = pytrends.trending_searches() print(df.head())
The output of this code :
0 0 Coronavirus vaccine news 1 Italy news 2 Maharashtra news 3 Zoom 4 Covid 19 vaccine update 5 Domestic flights 6 Covid out 7 Curfew in Delhi 8 Skype 9 Vivo V19
Related Queries
Similarly, you can see the searches related to a particular trend as well. You do this using the related_searches method.
#related topics pytrends.build_payload(kw_list=["Machine Learning"]) df = pytrends.related_topics() print(df['Machine Learning']['top'])
The output returns a dictionary, we see only the top searches related to Machine Learning.
Output:
value formatted value ... topic_title topic_type 0 100 100 ... Learning Topic 1 100 100 ... Machine learning Field of study 2 68 68 ... Artificial intelligence Field of study 3 9 9 ... Data Topic 4 7 7 ... Python Programming language 5 5 5 ... Algorithm Topic 6 4 4 ... Course Education 7 4 4 ... Deep learning Topic 8 3 3 ... Science Topic 9 3 3 ... Intelligence Topic 10 3 3 ... Machine Topic 11 3 3 ... Data science Field of study
There are various other filters available in this API such as – Related Queries, Top Charts, Suggestions, Historical Hourly Interest, etc.
This was a beginner level tutorial on how to track Google trends in Python using Pytrends. To track particular websites, you would need Scrappy or Beautifulsoup. More on that later.
For further explanation visit: https://pypi.org/project/pytrends/#api-methods
For more such related content visit: Locally Weighted Linear Regression in Python
Leave a Reply