PyTrends Keeps returning Google Response with error code 400

3.2k Views Asked by At

I've been trying to pull Google Trends data for a number of different keywords and have been looping over each keyword using pandas to build each individual payload. However, I keep getting a Google error code of 400 when I try using a particular row of the keyword. This is the code that I have currently:

import csv
import time
import pandas as pd
from random import randint
from pytrends.request import TrendReq

# set gmail credentials and path to extract data
 

# Login to Google. Only need to run this once, the rest of requests will use the same session.
pytrend = TrendReq()
keywordcsv = "nba.csv"
keywords = pd.read_csv(keywordcsv)
print(keywords)
for index, row in keywords.iterrows():
    print("Downloading Keyword #" + str(index))
    temp = str(row[0])
    pytrend.build_payload(kw_list=temp, timeframe="2013-11-24 2018-11-11", geo='US')
    time.sleep(randint(5, 10))
    null_df = pytrend.interest_over_time()
    print(null_df)
    null_df.to_csv(path_or_buf='blah.csv', mode='a', header=0)
2

There are 2 best solutions below

0
On

Looks like kw_list is maxxed out at 5 words at a time. Not sure why this is - perhaps due to google's rate limiting. You can try to play around with timeout & other params but the foolproof way around this is to cap the batch size at 5.

0
On

I had this error. Turns out you the maximum length of keywords to compare is 5.