如何使用请求模块跳过连接超时网址

时间:2018-11-04 10:07:58

标签: python python-3.x python-requests

嗨,我如何使用请求模块来浏览一堆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)

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