pytrends

repository·master·Indexed 25 days ago

https://github.com/generalmills/pytrends

An 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.

Tokens
2.3K
Snippets
15
Records
28
Agent score
87%

What's inside pytrends

  1. Initialize TrendReq to connect to Google

    master

    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)
  2. Get Multirange Interest Over Time

    master

    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()
  3. Build a payload for Google Trends

    master

    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='')
  4. Get Historical Hourly Interest

    master

    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)
  5. Configure TrendReq with proxies and timeouts

    master

    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})