[英]Pytrends: The request failed: Google returned a response with code 429
I'm using Pytrends to extract Google trends data, like:我正在使用 Pytrends 来提取 Google 趋势数据,例如:
from pytrends.request import TrendReq
pytrend = TrendReq()
pytrend.build_payload(kw_list=['bitcoin'], cat=0, timeframe=from_date+' '+today_date)
And it returns an error:它返回一个错误:
ResponseError: The request failed: Google returned a response with code 429.
I made it yesterday and for some reason it doesn't work now!我昨天成功了,但由于某种原因,它现在不起作用了! The source code from github failed too:来自 github 的源代码也失败了:
pytrends = TrendReq(hl='en-US', tz=360, proxies = {'https': 'https://34.203.233.13:80'})
How can I fix this?我怎样才能解决这个问题? Thanks a lot!非常感谢!
This one took a while but it turned out the library just needed an update.这花了一段时间,但事实证明库只需要更新。 You can check out a few of the approaches I posted here, both of which resulted in Status 429 Responses:您可以查看我在此处发布的一些方法,这两种方法都会导致状态 429 响应:
https://github.com/GeneralMills/pytrends/issues/243 https://github.com/GeneralMills/pytrends/issues/243
Ultimately, I was able to get it working again by running the following command from my bash prompt:最终,通过从 bash 提示符运行以下命令,我能够使其再次工作:
Run:跑:
pip install --upgrade --user git+https://github.com/GeneralMills/pytrends
For the latest version.对于最新版本。
Hope that works for you too.希望这对你也有用。
EDIT:编辑:
If you can't upgrade from source you may have some luck with:如果您无法从源代码升级,您可能会遇到以下问题:
pip install pytrends --upgrade
Also, make sure you're running git as an administrator if on Windows.另外,如果在 Windows 上,请确保您以管理员身份运行 git。
I had the same problem even after updating the module with pip install --upgrade --user git+https://github.com/GeneralMills/pytrends
and restart python.即使在使用pip install --upgrade --user git+https://github.com/GeneralMills/pytrends
更新模块并重新启动 python 后,我也遇到了同样的问题。
But, the issue was solved via the below method:但是,该问题已通过以下方法解决:
Instead of代替
pytrends = TrendReq(hl='en-US', tz=360, timeout=(10,25), proxies=['https://34.203.233.13:80',], retries=2, backoff_factor=0.1, requests_args={'verify':False})
Just ran:刚跑:
pytrend = TrendReq()
Hope this can be helpful!希望这会有所帮助!
TLDR; TLDR; I solved the problem with a custom patch我用自定义补丁解决了这个问题
The problem comes from the Google bot recognition system.问题来自谷歌机器人识别系统。 As other similar systems do, it stops serving too frequent requests coming from suspicious clients.与其他类似系统一样,它停止为来自可疑客户端的过于频繁的请求提供服务。 Some of the features used to recognize trustworthy clients are the presence of specific headers generated by the javascript code present on the web pages.用于识别可信客户端的一些功能是存在由网页上的 javascript 代码生成的特定标头。 Unfortunately, the python requests library does not provide such a level of camouflage against those bot recognition systems since javascript code is not even executed.不幸的是,由于 javascript 代码甚至没有被执行,python requests 库并没有对那些机器人识别系统提供这种级别的伪装。 So the idea behind my patch is to leverage the headers generated by my browser interacting with google trends.所以我的补丁背后的想法是利用我的浏览器与谷歌趋势交互生成的标题。 Those headers are generated by the browser meanwhile I am logged in using my Google account, in other words, those headers are linked with my google account, so for them, I am trustworthy.这些标题是在我使用我的 Google 帐户登录的同时由浏览器生成的,换句话说,这些标题与我的 google 帐户相关联,因此对它们来说,我是值得信赖的。
I solved in the following way:我通过以下方式解决了:
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)
通过 pip install 运行升级命令后,您应该重新启动 python 内核并重新加载 pytrend 库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.