DEV Community

LogicBase Systems
LogicBase Systems

Posted on

pytrends 429 TooManyRequestsError: why it happens and 3 ways to fix it (2026)

Disclosure: I built the Apify Actor mentioned in fix 3, so I'm biased. Apify links here are referral links. Fixes 1 and 2 don't need it at all.

Your Google Trends script worked yesterday. Today it stops with:

pytrends.exceptions.TooManyRequestsError: The request failed: Google returned a response with code 429
Enter fullscreen mode Exit fullscreen mode

Here is what that error means, what state pytrends is in today, and three ways to get past it, starting with the free ones.

Why it happens

pytrends doesn't use an official API. It sends the same requests the trends.google.com website sends, and Google rate-limits them. When Google answers with HTTP status 429 ("Too Many Requests"), pytrends raises TooManyRequestsError. Google doesn't publish the limit, so there is no safe number of requests per minute to aim for. The same script can work from your laptop and fail from a cloud server or a shared IP.

Two details from the pytrends 4.9.2 source that help:

  • TooManyRequestsError is a subclass of pytrends.exceptions.ResponseError. Catch TooManyRequestsError for 429s only, or ResponseError for any other failed response.
  • Every call is a request. Creating a TrendReq() makes one request (it fetches a Google cookie). build_payload() makes one, interest_over_time() makes one, and related_queries() makes one per keyword. A loop that creates a new TrendReq and a new payload for every keyword makes requests faster than it looks.

Is pytrends still maintained?

No. As of 18 November 2026:

  • The GeneralMills/pytrends repository is archived (read-only). Its last commit is from 22 April 2023.
  • The latest release on PyPI is 4.9.2, uploaded in April 2023.

So nobody is going to ship a fix for the 429s upstream. Google's own Trends API is still an alpha. Its page still says Google is "accepting applications for alpha testers", so you can't count on getting access.

Fix 1: send fewer requests, back off on 429, cache everything (free)

Most 429s go away if you stop sending requests the moment Google pushes back, and stop asking for data you already have:

  • Reuse one TrendReq object instead of creating one per keyword.
  • Batch up to five keywords per payload. pytrends' README allows up to five terms per build_payload. Note that batched keywords share one 0–100 scale (they are compared against each other), which is either exactly what you want or not, depending on the analysis.
  • Back off exponentially on TooManyRequestsError, with some random jitter, instead of retrying immediately.
  • Cache results on disk. Last year's weekly interest doesn't change, so fetch it once.
import json, random, time
from pathlib import Path
from pytrends.request import TrendReq
from pytrends.exceptions import TooManyRequestsError

CACHE = Path("trends_cache")
CACHE.mkdir(exist_ok=True)
pytrends = TrendReq(hl="en-US", tz=0)   # create once, reuse for every call

def interest(keywords, timeframe="today 12-m", geo="", max_tries=5):
    """Interest over time for up to 5 keywords, cached on disk, with backoff on 429."""
    name = f"{'+'.join(keywords)}_{timeframe}_{geo}".replace(" ", "_").replace("/", "-")
    path = CACHE / f"{name}.json"
    if path.exists():
        return json.loads(path.read_text())

    for attempt in range(max_tries):
        try:
            pytrends.build_payload(keywords, timeframe=timeframe, geo=geo)
            df = pytrends.interest_over_time()
            break
        except TooManyRequestsError:
            wait = 60 * 2 ** attempt + random.uniform(0, 15)   # ~1, 2, 4, 8, 16 minutes
            print(f"429, sleeping {wait:.0f}s")
            time.sleep(wait)
    else:
        raise RuntimeError(f"Still rate-limited after {max_tries} tries: {keywords}")

    data = {kw: {d.strftime("%Y-%m-%d"): int(v) for d, v in df[kw].items()}
            for kw in keywords if kw in df}
    path.write_text(json.dumps(data))
    return data

for batch in (["python", "rust"], ["golang"]):
    result = interest(batch)
    print({kw: list(series.items())[-1] for kw, series in result.items()})
    time.sleep(random.uniform(5, 10))   # pause between batches
Enter fullscreen mode Exit fullscreen mode

I ran this on 18 November 2026. The first run fetched both batches, and the second run read them from trends_cache/ without contacting Google. The wait times are a starting point, not a number Google publishes. If you still see 429s, make them longer.

This is the right fix for notebooks, weekly reports and anything that can run slowly.

Fix 2: pytrends' built-in retries and proxies (free, with a catch)

TrendReq takes retries, backoff_factor and proxies arguments. Its full signature in 4.9.2 is:

TrendReq(hl='en-US', tz=360, geo='', timeout=(2, 5), proxies='',
         retries=0, backoff_factor=0, requests_args=None)
Enter fullscreen mode Exit fullscreen mode

The catch: retries and backoff_factor crash on current urllib3. pytrends builds its retry policy with urllib3's old method_whitelist argument, which urllib3 2.x no longer accepts. On a fresh install (pytrends 4.9.2, requests 2.34.2, urllib3 2.8.0) the first request fails with:

TypeError: Retry.__init__() got an unexpected keyword argument 'method_whitelist'
Enter fullscreen mode Exit fullscreen mode

If you want them anyway, pin urllib3 below 2 in that environment:

pip install pytrends "urllib3<2"
Enter fullscreen mode Exit fullscreen mode
from pytrends.request import TrendReq

pytrends = TrendReq(
    hl="en-US", tz=0,
    timeout=(10, 25),
    retries=2, backoff_factor=0.5,
    proxies=["https://my-proxy.example:8080"],   # a list; HTTPS proxies with a port
)
Enter fullscreen mode Exit fullscreen mode

Things I confirmed in a venv with urllib3 1.26.20:

  • It works: the retry policy is accepted and normal requests go through.
  • Retries cover 429, 500, 502 and 504. When they run out, you get requests.exceptions.RetryError ("too many 429 error responses"), not TooManyRequestsError. If you turn retries on, catch both. (I checked this against a local test server that always answers 429. With retries=2 it was hit three times before the error.)
  • proxies must be a list. The README says only HTTPS proxies work and you need the port number. pytrends moves to the next proxy after each successful response, and drops a proxy from the list if it raises a proxy error while fetching the cookie.

Pinning an old urllib3 can clash with other packages, so keep it in its own virtualenv. Proxies help only if they are IPs Google isn't already throttling.

Fix 3: send only the blocked keywords to a hosted scraper

If you need the data now and don't want to manage proxies, keep pytrends for the requests that work and send only the 429s to a hosted service. I use my own Google Trends Scraper on Apify for this. It costs $0.003 per keyword result ($0.001 per "trending now" item, if you use that mode). Searches with no data and failed searches aren't charged, and users on Apify's Silver and Gold Store discount tiers get 10% and 20% off.

pip install -U pytrends apify-client
export APIFY_TOKEN="your-token-here"   # Apify Console → Settings → API & Integrations
Enter fullscreen mode Exit fullscreen mode
import os
from pytrends.request import TrendReq
from pytrends.exceptions import TooManyRequestsError
from apify_client import ApifyClient

def via_apify(keywords, timeframe="today 12-m", geo=""):
    client = ApifyClient(os.environ["APIFY_TOKEN"])
    run_input = {"keywords": list(keywords), "timeframe": timeframe}
    if geo:
        run_input["geos"] = [geo]
    run = client.actor("logicbase_systems/google-trends").call(run_input=run_input)
    out = {}
    for row in client.dataset(run.default_dataset_id).iterate_items():
        if row.get("status") == "ok":
            out[row["keyword"]] = {p["date"][:10]: p["value"] for p in row["interestOverTime"]}
    return out

def get_interest(keywords, timeframe="today 12-m", geo=""):
    pytrends = TrendReq(hl="en-US", tz=0)
    out, blocked = {}, []
    for kw in keywords:              # one keyword per request = each on its own 0-100 scale
        try:
            pytrends.build_payload([kw], timeframe=timeframe, geo=geo)
            df = pytrends.interest_over_time()
            out[kw] = {d.strftime("%Y-%m-%d"): int(v) for d, v in df[kw].items()}
        except TooManyRequestsError:
            blocked.append(kw)
    if blocked:
        out.update(via_apify(blocked, timeframe, geo))
    return out

series = get_interest(["python", "rust"])
print({kw: list(s.items())[-1] for kw, s in series.items()})
Enter fullscreen mode Exit fullscreen mode

Both paths return the same shape, {keyword: {"YYYY-MM-DD": value}}. I ran the snippet on 18 November 2026 both ways: once normally, with pytrends answering, and once with pytrends forced to raise TooManyRequestsError so both keywords went to Apify. The fallback run took about 3 seconds inside Apify and returned two 0–100 weekly series. The latest points were within one of the values pytrends had returned a minute earlier (Google Trends samples its data, so small differences between requests are normal). Two keywords cost $0.006 plus a tiny start fee. On a machine Google isn't throttling, the function never calls Apify at all.

Each row from the Actor also has a summary (average, peak, peak date, latest), related queries and interest by region. The full Google Trends in Python walkthrough covers comparisons, CSV export and trending-now by country, so I won't repeat it here.

Which one should you pick?

Situation Pick
Notebook, weekly report, anything that can run slowly Fix 1: reuse, batch, back off, cache
You already have good HTTPS proxies and can pin urllib3<2 Fix 2: pytrends' retries / proxies
Production job, low or bursty volume, no proxy setup Fix 3: fall back to a hosted scraper for the 429s only

One last thing: a ResponseError with a code other than 429, or an empty DataFrame, is not a rate limit, so slowing down won't fix it. Check your geo code, timeframe string and keyword first.

Top comments (0)