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

Ube*_*ber 1 python timeout timer urllib request

我做了一个使用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!")
Run Code Online (Sandbox Code Playgroud)

有没有办法使它每分钟只处理20个请求?还是如果不可能,我可以尝试20次后超时吗?(顺便说一句,这只是代码的一部分)

tha*_*man 5

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


jfs*_*jfs 5

time.sleep(3)保证您的代码每分钟不会发出超过 20 个请求,但它可能会不必要地延迟允许的请求:假设您只需要发出 10 个请求:time.sleep(3)在每个请求使循环运行半分钟后,api 允许您在这种情况下,一次发出所有 10 个请求(或至少一个接一个地发出)。

要在不延迟初始请求的情况下强制执行每分钟 20 个请求的限制,您可以使用RatedSemaphore(20, period=60)

rate_limit = RatedSemaphore(20, 60)
for hash_value in hash_file:
    with rate_limit, urlopen(make_url(hash_value)) as response:
        data = json.load(response)
Run Code Online (Sandbox Code Playgroud)

您甚至可以在遵守速率限制的同时同时发出多个请求:

from multiprocessing.pool import ThreadPool

def make_request(hash_value, rate_limit=RatedSemaphore(20, 60)):
    with rate_limit:
        try:
            with urlopen(make_url(hash_value)) as response:
                return json.load(response), None
        except Exception as e:
                return None, e


pool = ThreadPool(4) # make 4 concurrent requests
for data, error in pool.imap_unordered(make_request, hash_file):
    if error is None:
        print(data)
Run Code Online (Sandbox Code Playgroud)