Overview
Google Trends provides valuable insights into search trends and keyword popularity. However, Google doesn’t offer an official API for accessing this data. Fortunately, the Pytrends library enables us to access Google Trends data using Python.
In this tutorial, we’ll walk you through the installation and setup of Pytrends, and demonstrate how to perform a simple search and interpret the results.
Prerequisites
To follow this tutorial, you should have:
- Python 3 installed
- Basic knowledge of Python programming
- Familiarity with using Python packages
Installation
To install Pytrends, simply use pip:
pip install pytrends
Setting Up Pytrends
To start using Pytrends, first import the necessary libraries and establish a connection to Google Trends:
from pytrends.request import TrendReq
pytrends = TrendReq(hl='en-US', tz=360)
Here, we set the language to English (hl='en-US'
) and the timezone to UTC+0 (tz=360
).
Performing a Basic Search
Now, let’s perform a simple search to see the interest over time for the keyword “Python”:
keywords = ['Python']
pytrends.build_payload(keywords, timeframe='today 5-y', geo='', gprop='')
interest_over_time_df = pytrends.interest_over_time()
print(interest_over_time_df)
This code snippet defines a list of keywords, sets the timeframe to the past five years (timeframe='today 5-y'
), and leaves the geographic location and Google property empty. The interest_over_time()
method returns a DataFrame containing the interest data.
Understanding the Results
The resulting DataFrame contains the search interest for the keyword “Python” over the past five years. The values represent the search interest relative to the highest point in the specified timeframe, with 100 being the peak popularity.
Conclusion
In this post, we introduced Pytrends, an unofficial Google Trends API for Python, and demonstrated how to install and set it up. We performed a basic search using the library and discussed how to interpret the results.
In the upcoming posts, we’ll dive deeper into more advanced Pytrends functionalities, such as analyzing interest over time, exploring interest by region, and uncovering related topics and queries. Stay tuned!
NOTE : pytrends uses an unofficial API. Please use here for issues.
SAMPLE CODE : https://github.com/hobbyworker/google-trend-for-python