I'm using pytrends to extract query index from Google Trends for multiple countries over time. As it seems not possible to specify multiple countries using geo, I use Interest Over Time for each country and loop over a list of many countries.
The problems are
1) As I repeatedly run this same script (with waiting in between), each time the resulted csv files contain different subsets of countries. Some countries with seemingly normal values (much larger than 0) in some running will disappear entirely in another running.
2) Some countries with valid values when manually downloaded from Trends website are absent when running the script.
What may be the possible causes for these problems? Thanks a lot!
Code sample:
from pytrends.request import TrendReq
pytrend = TrendReq()
coun=('''AU
FI
GB
HU
IL
JP
NL
NZ
PT
US
''').split()
for country in coun:
try:
pytrend.build_payload(kw_list=['holiday'], timeframe='all', geo=country)
interest_over_time_df = pytrend.interest_over_time()
interest_over_time_df.to_csv(country+'_holiday.csv')
except:
continue
I know this is answer may be obvious, but have you checked if all of your requests are successful on each run? If you are running code that makes a lot of requests to API's you can easily hit their max request limit and get denied access. Test it with the following code:
from pytrends.request import TrendReq
pytrend = TrendReq()
coun=('''AU
FI
GB
HU
IL
JP
NL
NZ
PT
US
''').split()
for country in coun:
try:
pytrend.build_payload(kw_list=['holiday'], timeframe='all', geo=country)
interest_over_time_df = pytrend.interest_over_time()
interest_over_time_df.to_csv(country+'_holiday.csv')
print(country + ' was succesfully pulled from google trends and stored in ' + country + '_holiday.csv')
except Exception as e:
print(country + ' was not successfully pulled because of the following error: ' + str(e))
continue