Python:每分钟只需要请求20次(Python: Need to request only 20 times per minute)

编程入门 行业动态 更新时间:2024-10-26 02:27:43
Python:每分钟只需要请求20次(Python: Need to request only 20 times per minute)

我制作了一个使用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.

更多推荐

本文发布于:2023-07-05 07:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034887.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:只需要   每分钟   Python   minute   times

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!