【发布时间】:2022-01-06 21:19:17
【问题描述】:
我尝试使用 pytrends 来提取 Google 趋势数据。您可以在下面看到我使用的代码:
from pytrends import dailydata
words = ['aaa', 'bbb']
chunks = [words[i:i+1] for i in range(len(list_all))[::1]]
for chunk in chunks:
res = dailydata.get_daily_data(chunk, start_year=2020, start_mon=1, stop_year=2020, stop_mon=12, geo = '')
res.to_csv(chunk[0]+'.csv')
time.sleep(60)
如果没有 for 循环,一切都可以完美运行,但是一旦我使用 for 循环(我需要这样做,因为我有大量请求),我就会收到以下错误:
请求失败:Google 返回了代码为 400 的响应。 60 秒后重试。
你看出问题出在哪里了吗?
【问题讨论】:
-
Google 的 api 可能有请求限制,因此您只能在一定时间内向该 API 发送一定数量的请求。
-
他们确实有,但那是错误 429。我用 time.sleep(60) 修复了这个问题,通常不会导致任何错误。我之前一直在使用interest_over_time(),结果是这样的。
标签: python python-3.x for-loop server google-trends