Skip to main content
 首页 » 编程设计

Python:每分钟只需要请求 20 次

2025年05月04日8lyhabc

我制作了一个 python 代码,它使用一个 api 来请求一些数据,但是该 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 次尝试后让它超时吗? (顺便说一句,这只是代码的一部分)

请您参考如下方法:

您想使用time 模块。在每个循环的末尾添加一个 time.sleep(3),您将每分钟最多收到 20 个请求。