Skip to content

Instantly share code, notes, and snippets.

@pixyfox
Last active March 2, 2021 08:47
Show Gist options
  • Save pixyfox/469ee2e85fd3fc78aabf59b25086b420 to your computer and use it in GitHub Desktop.
Save pixyfox/469ee2e85fd3fc78aabf59b25086b420 to your computer and use it in GitHub Desktop.
def googletrends_queries(keywords, geo, loops=3, wait=10, csv='trends.csv'):
# Perform searches
print('Number of queries to do: ', len(keywords) * len(geo))
# Prepare containers
trends = dict.fromkeys(geo)
errors_list = []
cnt = 1
# Start loops
for g in geo:
trends[g] = {}
for k in keywords:
try:
time.sleep(wait)
pytrends.build_payload([k], timeframe='all', geo=g, gprop='')
trends[g][k] = pytrends.interest_over_time()[k]
#print(cnt, 'Success: ', g, k)
except:
print(cnt, ') Error: ', g, ' & ', k)
errors_list.append([g,k])
cnt+=1
### Redo searches with errors
if len(errors_list) > 0:
print('\nStart errors:')
for t in range(1,loops+1):
if len(errors_list) > 0:
print('Loop ', t, ': ', len(errors_list), 'errors')
for i in errors_list:
g = i[0] # 1st element geo
k = i[1] # 2nd element keyword
try:
time.sleep(wait)
pytrends.build_payload([k], timeframe='all',
geo=g, gprop='')
trends[g][k] = pytrends.interest_over_time()[k]
errors_list.remove([g,k]) # remove from list of errors
#print('Success: ', g, k)
except:
#print('*** Error: ', g, ' & ', k)
pass
print('\nDone -', len(errors_list), 'errors left')
### Save dataframe
dict_of_trends = {g: pd.DataFrame(k) for g,k in trends.items()}
data_df = pd.concat(dict_of_trends, axis=1)
data_df.to_csv(csv,index=False)
display(data_df.head()) # Check data
return data_df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment