简体   繁体   English

如何避免 python 中的 429 错误 requests.get()?

[英]how to avoid 429 error requests.get() in python?

I'm trying to get some data from pubg API using requests.get().我正在尝试使用 requests.get() 从 pubg API 获取一些数据。

While code was excuting, response.status_code returned 429.代码执行时,response.status_code 返回 429。

After I got 429, I couldn't get 200.拿到429后,200就拿不到了。

how to fix this situation?如何解决这种情况?

Here is part of my code.这是我的代码的一部分。

for num in range(len(platform)):
   url = "https://api.pubg.com/shards/"+platform[num]+"/players/"+playerID[num]+"/seasons/"+seasonID+"/ranked"
   req = requests.get(url, headers=header)
   print(req.status_code)
[output]
200
429

As per https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429根据https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429

you are sending too many requests at once/in short span of time.您一次/在短时间内发送了太多请求。 I recommend using time.sleep(10)我推荐使用time.sleep(10)

import time


for num in range(len(platform)):
   ....
   ....
   time.sleep(10)

I used 10 seconds, but you have to test it and understand how much time gap is requred.我用了 10 秒,但你必须测试它并了解需要多少时间间隔。 I would also recommend using https://pypi.org/project/retry/ after you figure out the right amount of sleep time.我还建议您在确定合适的睡眠时间后使用https://pypi.org/project/retry/

As mentioned by sam, HTTP error 429 means you are making too many requests in a certain amount of time.正如 sam 所提到的,HTTP 错误 429 意味着您在一定时间内发出了太多请求。

According to the official PUBG API documentation , the API actually tells you these rate limits by sending an additional header called X-RateLimit-Limit with every request.根据官方 PUBG API 文档,API 实际上通过在每个请求中发送一个名为X-RateLimit-Limit的额外 header 来告诉您这些速率限制。 Each request also has a header called X-RateLimit-Remaining which tells you how many requests you have currently left until the next rate reset which happens at the time specified in the third header X-RateLimit-Reset .每个请求还有一个名为X-RateLimit-Remaining的 header,它会告诉您当前还有多少请求,直到下一次速率重置发生在第三个 header X-RateLimit-Reset中指定的时间。

Since you seem to use the requests library, you can access these headers with a simple req.headers after making your request in python.由于您似乎使用了请求库,因此在 python 中发出请求后,您可以使用简单的req.headers访问这些标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM