WWH*_*932 2 python google-trends
我正在使用Pytrends来提取Google趋势数据,例如:
from pytrends.request import TrendReq
pytrend = TrendReq()
pytrend.build_payload(kw_list=['bitcoin'], cat=0, timeframe=from_date+' '+today_date)
Run Code Online (Sandbox Code Playgroud)
它返回一个错误:
ResponseError: The request failed: Google returned a response with code 429.
Run Code Online (Sandbox Code Playgroud)
我昨天做了,由于某种原因它现在不起作用!来自github的源代码也失败了:
pytrends = TrendReq(hl='en-US', tz=360, proxies = {'https': 'https://34.203.233.13:80'})
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?非常感谢!
Ant*_*uca 13
太长了;我用自定义补丁解决了这个问题
问题来自于谷歌机器人识别系统。与其他类似系统一样,它会停止服务来自可疑客户端的过于频繁的请求。用于识别可信客户端的一些功能是由网页上的 javascript 代码生成的特定标头的存在。不幸的是,Python requests 库没有针对这些机器人识别系统提供这种级别的伪装,因为 JavaScript 代码甚至没有被执行。因此,我的补丁背后的想法是利用我的浏览器与谷歌趋势交互生成的标头。这些标头是由浏览器生成的,同时我使用我的 Google 帐户登录,换句话说,这些标头与我的 Google 帐户链接,所以对他们来说,我是值得信赖的。
我通过以下方式解决了:
from pytrends.request import TrendReq as UTrendReq
GET_METHOD='get'
import requests
headers = {
...
}
class TrendReq(UTrendReq):
def _get_data(self, url, method=GET_METHOD, trim_chars=0, **kwargs):
return super()._get_data(url, method=GET_METHOD, trim_chars=trim_chars, headers=headers, **kwargs)
Run Code Online (Sandbox Code Playgroud)
这个花了一段时间,但事实证明图书馆只需要更新.您可以查看我在此处发布的一些方法,这两种方法都会导致状态429响应:
https://github.com/GeneralMills/pytrends/issues/243
最终,通过从我的bash提示符运行以下命令,我能够再次使用它:
跑:
pip install --upgrade --user git+https://github.com/GeneralMills/pytrends
对于最新版本.
希望对你也有用.
编辑:
如果您无法从源代码升级,您可能会有以下运气:
pip install pytrends --upgrade
此外,如果在Windows上,请确保您以管理员身份运行git.