Skip to content

Instantly share code, notes, and snippets.

import time
import pandas as pd
import pytrends
from pytrends.request import TrendReq
pytrends = TrendReq(hl='en-US', tz=0, retries=10)
# keywords
keywords = ['apple', 'facebook', 'bitcoin']
keywords.sort() # Sort the list
# locations
geo = ['US', 'AU', 'GB', 'DE']
geo.sort()
@pixyfox
pixyfox / block4.py
Last active November 12, 2022 14:08
# Perform searches
wait = 6 # in seconds
print('Number of queries to do: ', len(keywords) * len(geo))
# Prepare containers
trends = dict.fromkeys(geo)
errors_list = []
cnt = 1
# Start loops
# 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:
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('trends.csv',index=False)
data_df.head() # Check data
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
@pixyfox
pixyfox / block8.py
Last active November 25, 2023 15:41
data_df = googletrends_queries(keywords=keywords, geo=geo, loops=2)