PyTrends for keyword research

Learn how to utilize pytrends library to query google for trending keywords of your interest.

Unlocking the Power of Keyword Research with Pytrends: A Step-by-Step Guide Keyword research is an essential part of building a successful online business. Whether you’re crafting the perfect blog post, optimizing your product page, or designing a PPC campaign, knowing which search terms your audience uses is key.

One powerful tool you can use to gain insights into search trends is Pytrends, a Python wrapper for the Google Trends API. It allows you to extract and analyze Google search data, which can be used for keyword research, content planning, and understanding user intent. In this post, we’ll walk you through how to use Pytrends with practical code examples.

What is Pytrends? Pytrends is an unofficial API wrapper for Google Trends, enabling Python users to automate and extract data from Google Trends directly. With it, you can explore search term popularity, related queries, and even search trends over time in specific regions.

Setting Up Pytrends Before we dive into the code, let’s install Pytrends. You can install it using pip:

pip install pytrends

Once installed, you can start using Pytrends by importing the library and setting up a connection.

# Import required libraries
from pytrends.request import TrendReq

# Initialize Pytrends
pytrends = TrendReq(hl='en-US', tz=360)

In the above example:

hl stands for language (here, en-US for English). tz refers to the time zone, where 360 represents UTC+6. You can adjust these as per your requirements.

1. How to Get Interest Over Time for a Keyword

The most basic use of Pytrends is checking how a particular keyword’s popularity has changed over time.

Example: Get search interest for a keyword over time

# Define a keyword list
keywords = ["keyword research"]

# Build the payload
pytrends.build_payload(keywords, cat=0, timeframe='today 12-m', geo='US', gprop='')

# Get interest over time
interest_over_time_df = pytrends.interest_over_time()

# Display data
print(interest_over_time_df)
  • cat=0 specifies no category filter.
  • timeframe='today 12-m' gives data from the last 12 months.
  • geo='US' restricts the data to the United States (you can use other country codes as well).
  • gprop='' keeps the search on the web (you can set it to ‘youtube’, ‘images’, etc.). The result will be a Pandas DataFrame showing how interest in your chosen keyword(s) has evolved over the selected timeframe.

2. Finding Related Queries for a Keyword

Pytrends can also show you what related queries people are searching for, helping you discover secondary keywords or long-tail keyword opportunities.

# Define a keyword list
keywords = ["content marketing"]

# Build the payload
pytrends.build_payload(keywords, cat=0, timeframe='today 12-m', geo='US', gprop='')

# Get related queries
related_queries = pytrends.related_queries()

# Display related queries for the first keyword
print(related_queries['content marketing']['top'])

This returns a DataFrame of the most popular related queries along with their relative search volumes. The top category contains queries with the highest search volume, while rising contains those that are rapidly gaining popularity.

3. Keyword Interest by Region

If your content is geographically focused or you’re running local SEO campaigns, it’s helpful to know which regions have the highest interest in your target keywords.

Example: Find keyword interest by region

# Define a keyword list
keywords = ["digital marketing"]

# Build the payload
pytrends.build_payload(keywords, cat=0, timeframe='today 12-m', geo='US', gprop='')

# Get interest by region
interest_by_region_df = pytrends.interest_by_region(resolution='COUNTRY')

# Display data
print(interest_by_region_df.sort_values(by='digital marketing', ascending=False).head(10))

This returns the interest level of the keyword “digital marketing” across different countries or states. You can adjust the resolution parameter to “CITY” or “DMA” for more granular location data.

4. Analyzing Related Topics

Apart from related queries, Pytrends allows you to discover broader related topics that are being searched alongside your target keyword.

# Define a keyword list
keywords = ["SEO"]

# Build the payload
pytrends.build_payload(keywords, cat=0, timeframe='today 12-m', geo='US', gprop='')

# Get related topics
related_topics = pytrends.related_topics()

# Display related topics for the first keyword
print(related_topics['SEO']['top'])

This function returns a DataFrame of related topics, which can help you better understand the content and subjects people are searching for in relation to your keyword.

5. Comparing Multiple Keywords

You can also compare multiple keywords to see how their popularity stacks up against one another.

Example: Compare multiple keywords

# Define a keyword list
keywords = ["SEO", "PPC", "content marketing"]

# Build the payload
pytrends.build_payload(keywords, cat=0, timeframe='today 12-m', geo='US', gprop='')

# Get interest over time for multiple keywords
interest_comparison_df = pytrends.interest_over_time()

# Display data
print(interest_comparison_df)

The resulting DataFrame will show how each keyword’s interest has varied over time, making it easy to spot trends or seasonal changes in popularity.

6. Downloading Keyword Data as CSV

Once you’ve gathered your keyword data, you can export it for further analysis.

Example: Export data to CSV

# Export interest over time to CSV
interest_over_time_df.to_csv('interest_over_time.csv')

# Export related queries to CSV
related_queries['content marketing']['top'].to_csv('related_queries.csv')

This lets you share the data with your team or import it into other tools for further processing.

Conclusion

Pytrends is a powerful, flexible tool for performing keyword research and analyzing search trends. Whether you want to track keyword popularity over time, discover related queries, or target specific geographic regions, Pytrends gives you access to valuable data straight from Google Trends.

With the help of Pytrends, your keyword research strategy can become data-driven, efficient, and highly targeted—helping your content and SEO efforts hit the mark.

Next Steps

Now that you know how to use Pytrends for keyword research, try incorporating it into your marketing workflow. Set up a regular routine of checking search trends, discovering related queries, and refining your keyword lists. Over time, you’ll gain deeper insights into your audience’s search behavior, giving you the competitive edge.

Happy researching!

Cookies
essential