简体   繁体   中英

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

I'm trying to get some data from pubg API using requests.get().

While code was excuting, response.status_code returned 429.

After I got 429, I couldn't get 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

you are sending too many requests at once/in short span of time. I recommend using 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. I would also recommend using https://pypi.org/project/retry/ after you figure out the right amount of sleep time.

As mentioned by sam, HTTP error 429 means you are making too many requests in a certain amount of time.

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. 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 .

Since you seem to use the requests library, you can access these headers with a simple req.headers after making your request in python.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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