如何为不存在的网站捕获Python 3中的错误

时间:2018-07-19 22:19:55

标签: python-3.6 http-error urllib3 connectionexception

我正在尝试使用文本文件中的URL下载图像数据集。所有数据均已正确加载,但是当它到达无效URL时,尽管我有try try块来捕获它,但仍收到ConnectionError。我用请求和urllib3.exceptions尝试了多种方法来捕获错误,但我无法解决它。 任何帮助将不胜感激。

    num_lines = len(images)
    i = 0


    #downloads images and randomly assigns them to test to train, but is 
    biased towards training
for key in images:
    print(str(round(i/num_lines*100,2))+"%")
    i+=1

    try:
        time.sleep(.5)
        #print(urlOk(images[key]))
        #print (urllib.request.urlopen("http://www.stackoverflow.com").getcode())        
        r= requests.head(images[key])
        print(r)
        img_data = requests.get(images[key]).content
        print("image downloaded")
        testPath = 'C:/Vijay/images/test/'
        trainPath = 'C:/Vijay/images/train/'
        pathMaker = random.randint(1,101)
        if pathMaker >70:
            if not os.path.exists(testPath+key[:9]):
                os.makedirs(testPath+key[:9])
            os.chdir(testPath+key[:9])
                name = key+'.jpg'
                with open(name, 'wb') as handler:
                    handler.write(img_data)
        else:
            if not os.path.exists(trainPath+key[:9]):
                os.makedirs(trainPath+key[:9])
            os.chdir(trainPath+key[:9])
            name = key
            with open(name, 'wb') as handler:
                handler.write(img_data)
    except ConnectionError:
        print('invalid url, moving to next entry')
        continue

抛出错误的URL的示例是 http://macip-pracht.com/images/abacus.jpg

0 个答案:

没有答案