0

I am using the pytrends related_queries() function to find related keywords for a list of keywords but if one of the keywords doesn't have enough data to provide related queries the process crashes with the error:

KeyError: "[u'query' u'value'] not in index"

After the error has occurred the code won't work for any keyword until I shut down and restart the Kernel so I haven't had any success in trying to catch the error. An example that doesn't work is:

pytrend.build_payload(kw_list=['AMX HOME AUTOMATION'])
Related_df = pytrend.related_queries()
DarkCygnus
  • 7,420
  • 4
  • 36
  • 59
Jon.G
  • 127
  • 2
  • 11

1 Answers1

0

I was able to catch the error by re-establishing the connection for every iteration, it's inefficient but got the Job done:

r = pd.DataFrame()
s = pd.DataFrame()
for i in listofwords:
    try:
        pytrend = TrendReq(google_username, google_password, custom_useragent='Pytrends')
        pytrend.build_payload(kw_list=[i])
        Related_df = pytrend.related_queries()
        s = s.append(Related_df[i]['top'].head(3))
        r = r.append(Related_df[i]['rising']['query'].head(3))
    except:
        pass
FearlessHyena
  • 3,527
  • 33
  • 39
Jon.G
  • 127
  • 2
  • 11