This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import pandas as pd | |
import pytrends |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pytrends.request import TrendReq | |
pytrends = TrendReq(hl='en-US', tz=0, retries=10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# keywords | |
keywords = ['apple', 'facebook', 'bitcoin'] | |
keywords.sort() # Sort the list | |
# locations | |
geo = ['US', 'AU', 'GB', 'DE'] | |
geo.sort() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data_df = googletrends_queries(keywords=keywords, geo=geo, loops=2) |