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.
pip install pytrendsVerified import paths — ran on the pinned version, not inferred.
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'.
Always check for the latest `pytrends` version and consult the GitHub repository for recent fixes or workarounds. Installing directly from GitHub might be necessary for the latest patches.
Introduce delays between requests (`time.sleep()`), reduce the number of requests, use proxies, or configure `retries` and `backoff_factor` when initializing `TrendReq`. Consider batching requests.
Always include all keywords you wish to compare within a single `kw_list` parameter when calling `build_payload()`.
Carefully determine the correct `tz` value for your target region. For example, US Central Standard Time (UTC-6) is `tz=360`. Refer to `pytrends` documentation or community resources for a list of common `hl` and `tz` pairs.
Review your `kw_list`, `timeframe`, `geo`, `cat`, and `gprop` parameters to ensure they are valid and correctly formatted according to Google Trends' expectations.
Implement request throttling by setting the 'retries' and 'backoff_factor' parameters in the TrendReq initialization to manage the frequency of requests. For example: 'pytrends = TrendReq(retries=3, backoff_factor=0.1)'.
Retry the request after a short delay. If the error persists, check for any updates or issues with the pytrends library that might be causing the problem.
Before accessing the DataFrame, check if the 'top' and 'rising' keys exist in the dictionary returned by 'related_queries()'. If they don't, handle the absence gracefully to prevent the KeyError.
Create an instance of 'TrendReq' first, then call 'trending_searches()' on that instance. For example: 'pytrend = TrendReq(); trending_searches_df = pytrend.trending_searches(pn="united_states")'.
Install the 'pytrends' library using pip: 'pip install pytrends'.