Pytrends:重复 运行 同一脚本给出不同国家/地区的结果
Pytrends: repeatedly running the same script gives results for different sets of countries
我正在使用 pytrends 从 Google 多个国家/地区随时间变化的趋势中提取查询索引。
由于似乎无法使用 geo 指定多个国家/地区,因此我对每个国家/地区使用随时间变化的利息并循环遍历许多国家/地区的列表。
问题是
1) 当我重复 运行 相同的脚本(中间等待)时,每次生成的 csv 文件都包含不同的国家子集。一些在某些 运行ning 中看似正常值(远大于 0)的国家将在另一个 运行ning 中完全消失。
2) 运行 脚本时,某些从趋势网站手动下载时具有有效值的国家/地区不存在。
这些问题的可能原因是什么?非常感谢!
代码示例:
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
我知道这个答案可能很明显,但是您是否检查过每个 运行 上的所有请求是否都成功?如果您的 运行ning 代码向 API 发出大量请求,您很容易达到最大请求限制并被拒绝访问。使用以下代码对其进行测试:
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
我正在使用 pytrends 从 Google 多个国家/地区随时间变化的趋势中提取查询索引。 由于似乎无法使用 geo 指定多个国家/地区,因此我对每个国家/地区使用随时间变化的利息并循环遍历许多国家/地区的列表。
问题是
1) 当我重复 运行 相同的脚本(中间等待)时,每次生成的 csv 文件都包含不同的国家子集。一些在某些 运行ning 中看似正常值(远大于 0)的国家将在另一个 运行ning 中完全消失。
2) 运行 脚本时,某些从趋势网站手动下载时具有有效值的国家/地区不存在。
这些问题的可能原因是什么?非常感谢!
代码示例:
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
我知道这个答案可能很明显,但是您是否检查过每个 运行 上的所有请求是否都成功?如果您的 运行ning 代码向 API 发出大量请求,您很容易达到最大请求限制并被拒绝访问。使用以下代码对其进行测试:
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