我制作了一个使用api请求某些数据的python代码,但api只允许每分钟发出20个请求。 我正在使用urllib来请求数据。 此外,我正在使用for循环,因为数据位于文件中:
for i in hashfile: hash = i url1 = "https://hashes.org/api.php?act=REQUEST&key="+key+"&hash="+hash print(url1) response = urllib.request.urlopen(url2).read() strr = str(response) if "plain" in strr: parsed_json = json.loads(response.decode("UTF-8")) print(parsed_json['739c5b1cd5681e668f689aa66bcc254c']['plain']) writehash = i+parsed_json hashfile.write(writehash + "\n") elif "INVALID HASH" in strr: print("You have entered an invalid hash.") elif "NOT FOUND" in strr: print("The hash is not found.") elif "LIMIT REACHED" in strr: print("You have reached the max requests per minute, please try again in one minute.") elif "INVALID KEY!" in strr: print("You have entered a wrong key!") else: print("You have entered a wrong input!")有没有办法让它每分钟只做20个请求? 或者如果这是不可能的,我可以让它在20次尝试后超时? (顺便说一句,它只是代码的一部分)
I have made a python code that uses a api to request some data, but the api only allows for 20 requests per minute. I am using urllib to request the data. Also I am using a for loop because the data is located in a file:
for i in hashfile: hash = i url1 = "https://hashes.org/api.php?act=REQUEST&key="+key+"&hash="+hash print(url1) response = urllib.request.urlopen(url2).read() strr = str(response) if "plain" in strr: parsed_json = json.loads(response.decode("UTF-8")) print(parsed_json['739c5b1cd5681e668f689aa66bcc254c']['plain']) writehash = i+parsed_json hashfile.write(writehash + "\n") elif "INVALID HASH" in strr: print("You have entered an invalid hash.") elif "NOT FOUND" in strr: print("The hash is not found.") elif "LIMIT REACHED" in strr: print("You have reached the max requests per minute, please try again in one minute.") elif "INVALID KEY!" in strr: print("You have entered a wrong key!") else: print("You have entered a wrong input!")Is there a way to make it just do 20 requests per minute? or if that isn't possible can I make it timeout after 20 tries? (btw, it's just part of the code)
最满意答案
你想使用time模块。 在每个循环的末尾添加一个time.sleep(3) ,每分钟最多可以获得20个请求。
You want to use the time module. Add a time.sleep(3) at the end of each loop and you will get 20 requests a minute max.
更多推荐
发布评论