Install pytrends via pip
masterInstall the pytrends package using pip.
pip install pytrendsrepository·master·Indexed 25 days ago
https://github.com/generalmills/pytrendsAn unofficial Python API for Google Trends (version 4.9.2) that allows users to automate the downloading of interest reports, trending searches, and related queries. It provides tools to fetch interest over time, interest by region, related topics, and real-time search trends, returning data primarily as pandas DataFrames. The library includes the TrendReq client for connection management, payload building for queries, and specialized functions like get_daily_data() for scaled daily search volume.
Install the pytrends package using pip.
pip install pytrendsReturns realtime search trends for a specific country code (pn). Returns a pandas.DataFrame.
pytrends.realtime_trending_searches(pn='US')Returns historical, indexed data for when the keyword was searched most, as shown in the Google Trends 'Interest Over Time' section. Returns a pandas.DataFrame.
pytrends.interest_over_time()Use TrendReq from pytrends.request to establish a connection. You can specify the host language (hl) and the timezone offset (tz).
from pytrends.request import TrendReq
pytrends = TrendReq(hl='en-US', tz=360)Returns historical, indexed data across multiple time date ranges. The first row of the returned pandas.DataFrame includes the average. You must first call build_payload with a list of timeframes.
pytrends.build_payload(kw_list=['pizza', 'bagel'], timeframe=['2022-09-04 2022-09-10', '2022-09-18 2022-09-24'])
pytrends.multirange_interest_over_time()Before calling most API methods, you must build a payload using build_payload. This defines the keywords and filters for the subsequent request.
kw_list = ["Blockchain"]
pytrends.build_payload(kw_list, cat=0, timeframe='today 5-y', geo='', gprop='')Returns historical, indexed, hourly data. This method sends multiple requests to Google (one per week). Use the sleep parameter to avoid rate limits.
pytrends.get_historical_interest(kw_list, year_start=2018, month_start=1, day_start=1, hour_start=0, year_end=2018, month_end=2, day_end=1, hour_end=0, cat=0, geo='', gprop='', sleep=0)Returns the data for a given year. Note that Google does not return data for the current year and does not support monthly queries. Returns a pandas.DataFrame.
pytrends.top_charts(date, hl='en-US', tz=300, geo='GLOBAL')Returns data for the related queries to a provided keyword. Returns a dictionary of pandas.DataFrames.
pytrends.related_queries()Returns data for the related topics to a provided keyword. Returns a dictionary of pandas.DataFrames.
pytrends.related_topics()If you encounter Google rate limits, you can initialize TrendReq with proxies, timeouts, retries, and backoff factors. Note that only https proxies are supported and must include the port number.
from pytrends.request import TrendReq
pytrends = TrendReq(hl='en-US', tz=360, timeout=(10,25), proxies=['https://34.203.233.13:80',], retries=2, backoff_factor=0.1, requests_args={'verify':False})Returns a list of additional suggested keywords that can be used to refine a trend search. Returns a dictionary.
pytrends.suggestions(keyword)