【发布时间】:2016-01-17 01:25:30
【问题描述】:
我制作了一个使用 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 次尝试后让它超时吗? (顺便说一句,这只是代码的一部分)
【问题讨论】:
标签: python timer request timeout urllib