Google Trends API in Python: extract Google Trends data with apify-client

The Google Trends API runs from Python with the apify-client library: install it with pip, call the Actor kwerix/google-trends-api with a keyword list and read one row per keyword from the run's dataset. The same script returns the trend, the seasonality, the breakout queries and the regions, and a max_total_charge_usd option caps what the run can cost.

KwerixPublished

Python code that calls the Google Trends API in Python with apify-client and prints the trend per keyword

There are 4 steps to call the Google Trends API from Python.

  1. Install the client with pip install apify-client.
  2. Copy your API token from Apify Console → Settings → API & Integrations.
  3. Call the Actor kwerix/google-trends-api with a keyword list, a location and a time range.
  4. Iterate over the items of the run’s dataset, one row per keyword.
import os
from decimal import Decimal
from apify_client import ApifyClient

client = ApifyClient(os.environ["APIFY_TOKEN"])
run = client.actor("kwerix/google-trends-api").call(
    run_input={"keywords": ["sunscreen", "retinol"], "geo": "US"},
    max_total_charge_usd=Decimal("0.50"),
)
for row in client.dataset(run["defaultDatasetId"]).iterate_items():
    print(row["keyword"], row["trendSlopePctPerYear"], row["breakoutQueries"][:3])

Which fields does the Python client receive?

The Python client receives 6 groups of fields in every row of the Google Trends API.

FieldTypeExample (sunscreen)
averageInterest, peakDatefloat, date26.3, 2026-06-07
trendSlopePctPerYear, yoyChangePctfloat21.6, 55.9
seasonalitydict{"isSeasonal": True, "publishBy": "2027-04-24"}
breakoutQuerieslist of str["beauty of joseon", …]
risingQueries, topQuerieslist of dict{"query": "best sunscreen", "value": 100}
interestByRegionlist of dict{"geoName": "Wyoming", "value": 100}

How to compare keywords in Python

Comparing keywords in Python takes one switch: compareKeywords, with an anchor keyword of medium popularity.

rows = run({"keywords": keywords, "geo": "US", "timeRange": "today 12-m",
            "compareKeywords": True, "anchorKeyword": "retinol", "includeRelatedQueries": False})
ranked = sorted((r for r in rows if r.get("comparison")), key=lambda r: r["comparison"]["rank"])
for r in ranked:
    print(r["comparison"]["rank"], r["keyword"], r["comparison"]["averageOnCommonScale"])

Which Python examples are ready to run?

4 Python examples are ready to run in the Kwerix repository on GitHub, under the MIT license.

  1. keyword_analysis.py: trend, year-over-year change, seasonality and breakout queries for a keyword list.
  2. compare_keywords.py: any number of keywords ranked on one scale.
  3. seasonal_calendar.py: an SEO content calendar with the keywords to publish for and the dates.
  4. trending_now.py: what is trending now in one or more countries, by category.

How to cap the cost of a Python run

A Python run caps its cost with max_total_charge_usd, which stops charging at the amount set; at $0.0015 per keyword, Decimal("0.50") covers up to 333 keywords.

Is there an official Google Trends library for Python?

No. Google publishes no Python library for Google Trends; pytrends was unofficial and archived on April 17, 2025, and the official API is an alpha with access by application.

Does the Google Trends API need pandas in Python?

No. The rows come back as Python dictionaries, and pandas is optional for anyone who wants a DataFrame.

Sources