pythongoogle-trendshttp-status-code-429

Pytends API throwing 429 error even if the request was made very first time


I am using the very simple code to find the data of the keyword by region. But everytime i ran it, it gives me 429 error, prompting that too many requests have been made but infact the request was made very first time and never before. The error i am getting is mentioned below.

raise exceptions.TooManyRequestsError.from_response(response) pytrends.exceptions.TooManyRequestsError: The request failed: Google returned a response with code 429

Here is the code, I am running.

import pandas as pd                        
from pytrends.request import TrendReq

pytrend = TrendReq()
kw_list = ["Blockchain"]
pytrend.build_payload(kw_list, cat=0, timeframe='today 12-m', geo='', gprop='')
# Interest by Region
df = pytrend.interest_by_region()
df.head(10)

Solution

  • Ok, tprogrammer, I think I might have found an answer for you, though I got it from this current github thread: Exception occurred: The request failed: Google returned a response with code 429

    ///Edit(21.03.23): There is a much more concise solution from user "jesvinc" there that lifts the request limits all around to the point it had been before. Apparently it had to do with the GetGoogleCookie method, which needs a little change. If you go into your python module folder (mine was at User\AppData\Roaming\Python\Python39\site-packages), inside pytrends you will find the file request.py. Open it and change the 'get' to 'post' in line 88 like this:

    return dict(filter(lambda i: i[0] == 'NID', requests.get(
    

    to

    return dict(filter(lambda i: i[0] == 'NID', requests.post(
    

    After this change, even the occasional remaining problems also disappeared, and it started to work with VPN again as well. This fix will most likely be in some form included in the next pytrends update, but until then, this works fine.