pytrends

4.9.2 · active · verified Sun Apr 12

Pytrends is an unofficial Python library that provides a pseudo-API for Google Trends, allowing users to automate the downloading of search interest data. The current version, 4.9.2, was released in April 2023. Due to its unofficial nature, it does not adhere to a fixed release cadence but rather updates as needed to adapt to changes in Google's backend, which can frequently introduce breaking changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `TrendReq` object, build a search payload for multiple keywords, and retrieve 'Interest Over Time' and 'Interest by Region' data from Google Trends. The data is returned as pandas DataFrames. Note the `tz` parameter for timezone offset, where '360' represents UTC-6 (e.g., US Central Standard Time) and not '-360'.

from pytrends.request import TrendReq
import pandas as pd

# Initialize pytrends object
# hl: host language, tz: timezone offset (e.g., 360 for US CST, NOT -360)
pytrends = TrendReq(hl='en-US', tz=360)

# Define keyword list
kw_list = ["Python programming", "Data Science"]

# Build payload for the request
# timeframe: e.g., 'today 5-y' for last 5 years
pytrends.build_payload(kw_list, cat=0, timeframe='today 5-y', geo='US', gprop='')

# Get Interest Over Time data
interest_over_time_df = pytrends.interest_over_time()

# Print the first few rows of the DataFrame
print("Interest Over Time (first 5 rows):")
print(interest_over_time_df.head())

# Get Interest by Region data
interest_by_region_df = pytrends.interest_by_region(resolution='COUNTRY', inc_low_vol=True, inc_geo_code=True)
print("\nInterest By Region (first 5 rows):")
print(interest_by_region_df.head())

view raw JSON →