嗨,我如何使用请求模块来浏览一堆URL,如果列表中的URL需要花费更多时间加载或连接超时,我如何跳过该特定URL并跳至下一个URL
def req():
with open('demofile.txt','r') as http:
for url in http.readlines():
req = url.strip()
print(req)
page=requests.get("http://"+req,verify=False)
if page.status_code == 400:
break
else:
continue
time.sleep(1)
答案 0 :(得分:3)
如果超时,则可以引发异常,并在finally
块继续进行下一个请求,
import requests
import logging
timeout = 0.00001
try:
response = requests.get(url="https://google.com", timeout=timeout)
except requests.exceptions.ConnectTimeout as e:
logging.error("Time out!")
finally:
# continue request here
print("hello")
# output,
ERROR:root:Time out!
hello