Pytrends library with Python 3.12.7 - Frequent, intermittent Google 429 errors - Stack Overflow

admin2025-04-21  2

I'm teaching my class how to do data analysis in Python. We are attempting to use Pytrends to gather datasets for the students to analyze.

The first day of the lesson, pytrends worked great. However, we are now getting frequent, intermittent Google 429 (too many requests) errors.

We've followed some online suggestions to fix the error including setting retries=3 (or greater), clearing cache and cookies, and modifying the requests.py file in the pytrends library. However, the 429 error persists.

Questions:

  1. Since we are all using the pytrends library from with inside the school's network could we be hitting our maximum number of requests not based on an individual student but based off the network IP address?
  2. Any other suggestions for getting this to work aside from what we tried above?

Students are using this basic code with the keyword list containing items they are interested in:

pytrend = TrendReq(retries=3)
kw = ["cookies", "pie", "cake"]
pytrend.build_payload(kw)
try:
    df = pytrend.interest_over_time()
except Exception:
    print("Error")

I'm teaching my class how to do data analysis in Python. We are attempting to use Pytrends to gather datasets for the students to analyze.

The first day of the lesson, pytrends worked great. However, we are now getting frequent, intermittent Google 429 (too many requests) errors.

We've followed some online suggestions to fix the error including setting retries=3 (or greater), clearing cache and cookies, and modifying the requests.py file in the pytrends library. However, the 429 error persists.

Questions:

  1. Since we are all using the pytrends library from with inside the school's network could we be hitting our maximum number of requests not based on an individual student but based off the network IP address?
  2. Any other suggestions for getting this to work aside from what we tried above?

Students are using this basic code with the keyword list containing items they are interested in:

pytrend = TrendReq(retries=3)
kw = ["cookies", "pie", "cake"]
pytrend.build_payload(kw)
try:
    df = pytrend.interest_over_time()
except Exception:
    print("Error")
Share Improve this question edited Jan 23 at 8:22 Mark Rotteveel 110k229 gold badges156 silver badges225 bronze badges asked Jan 22 at 22:57 Ann RootAnn Root 211 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

This is a common issue that has also been reported by individual users and it seems to be as a result of actions taken by Google against spam bots as suggested in this issue.

Do note that pytrends is an Unofficial API for Google Trends and the error code 429 indicates that your requests to the Google Trends API have exceeded the rate limit.

Some temporary fix that has been suggested includes; introducing a delay between requests, reducing the number of requests, using batch processing or simply using proxies.

See if any of these closed issues helps.

转载请注明原文地址:http://anycun.com/QandA/1745224719a90445.html