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)
I had this error. Turns out you the maximum length of keywords to compare is 5.