Pytends API 即使在首次请求时也会抛出 429 错误。

huangapple go评论65阅读模式
英文:

Pytends api throwing 429 error even if the request was made very first time

问题

以下是翻译好的部分:

我正在使用非常简单的代码按地区查找关键词的数据。但每次运行它时,都会出现429错误,提示已经发出了太多请求,但实际上请求是第一次发出的,之前从未发出过。我收到的错误信息如下。

> raise exceptions.TooManyRequestsError.from_response(response) pytrends.exceptions.TooManyRequestsError: 请求失败:Google返回代码429的响应

以下是我正在运行的代码。

import pandas as pd
from pytrends.request import TrendReq

pytrend = TrendReq()
kw_list = ["Blockchain"]
pytrend.build_payload(kw_list, cat=0, timeframe='today 12-m', geo='', gprop='')
# 按地区的兴趣
df = pytrend.interest_by_region()
df.head(10)
英文:

I am using the very simple code to find the data of the keyword by region. But everytime i ran it, it gives me 429 error, prompting that too many requests have been made but infact the request was made very first time and never before. The error i am getting is mentioned below.

> raise exceptions.TooManyRequestsError.from_response(response)
pytrends.exceptions.TooManyRequestsError: The request failed: Google returned a response with code 429

Here is the code, i am running.

import pandas as pd                        
from pytrends.request import TrendReq





pytrend = TrendReq()
kw_list = ["Blockchain"]
pytrend.build_payload(kw_list, cat=0, timeframe='today 12-m', geo='', gprop='')
# Interest by Region
df = pytrend.interest_by_region()
df.head(10)

答案1

得分: 1

好的,以下是您要翻译的部分:

Ok,tprogrammer,我认为我可能已经为您找到了一个答案,尽管我是从这个当前的github主题中获得的:异常发生:请求失败:Google返回了代码429的响应

///编辑(21.03.23):
用户“jesvinc”提供了一个更为简洁的解决方案,可以将请求限制恢复到之前的状态。
显然,这与“GetGoogleCookie”方法有关,需要稍作更改。如果您进入您的Python模块文件夹(我的在User\AppData\Roaming\Python\Python39\site-packages),在pytrends文件夹内,您会找到request.py文件。打开它,并像这样在第88行将'get'更改为'post':

return dict(filter(lambda i: i[0] == 'NID', requests.get(

更改为

return dict(filter(lambda i: i[0] == 'NID', requests.post(

经过这个更改,甚至偶尔出现的问题也消失了,它开始能够再次与VPN一起使用。这个修复很可能会以某种形式包含在下一个pytrends更新中,但在那之前,这个方法可以正常运行。

英文:

Ok, tprogrammer, I think I might have found an answer for you, though I got it from this current github thread: Exception occurred: The request failed: Google returned a response with code 429

///Edit(21.03.23):
There is a much more concise solution from user "jesvinc" there that lifts the request limits all around to the point it had been before.
Apparently it had to do with the GetGoogleCookie method, which needs a little change. If you go into your python module folder (mine was at User\AppData\Roaming\Python\Python39\site-packages), inside pytrends you will find the file request.py. Open it and change the 'get' to 'post' in line 88 like this:

return dict(filter(lambda i: i[0] == 'NID', requests.get(

to

return dict(filter(lambda i: i[0] == 'NID', requests.post(

After this change, even the occasional remaining problems also disappeared, and it started to work with VPN again as well. This fix will most likely be in some form included in the next pytrends update, but until then, this works fine.

huangapple
  • 本文由 发表于 2023年3月15日 20:12:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定