我做了一个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.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)
你甚至可以在遵守速率限制的情况下同时发出多个请求:
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)
您需要使用time
模块。在每个循环的末尾添加一个time.sleep(3)
,您将最多每分钟收到20个请求。