How to avoid 429 error requests.get() in python?
Question:
I’m trying to get some data from pubg API using requests.get()
.
While code was executing, 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
Answers:
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.
I’m trying to get some data from pubg API using requests.get()
.
While code was executing, 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.