gpt4 book ai didi

python - 使用Python通过urllib下载图片并测试网站是否存在

转载 作者:行者123 更新时间:2023-11-30 09:53:27 25 4
gpt4 key购买 nike

我正在编写一个 python 脚本,为我正在开发的项目下载训练图像,并且我正在编写一个 python 脚本来从 image-net 下载图像。下面是我正在运行的内容,但我不断收到此错误:

IOError: [Errno socket error] [Errno -2] Name or service not known

当它尝试访问: http://http://omusinternational.com/db3/00225/omusinternational.com/_uimages/oilRefinery.JPG 时.

我不知道如何修复它或告诉它扔掉它并继续处理列表中的下一项。我的代码如下:

#!/usr/bin/python

import urllib
import cv2
import numpy as np
import os
import httplib

neg_images_link = 'http://image-net.org/api/text/imagenet.synset.geturls?wnid=n03844673'
neg_image_urls = urllib.urlopen(neg_images_link).read().decode()
pic_num = 1



for i in neg_image_urls.split('\n'):
try:
print(i)
r = urllib.urlopen(i).getcode()
print r
if r == 200:
urllib.urlretrieve(i, str(pic_num)+".jpg")
img = cv2.imread(str(pic_num)+".jpg",cv2.IMREAD_GRAYSCALE)
# should be larger than samples / pos pic (so we can place our image on it)
if img != None:
resized_image = cv2.resize(img, (100, 100))
cv2.imwrite(str(pic_num)+".jpg",resized_image)
pic_num += 1

except KeyboardInterrupt:
print "Closing"

最佳答案

由于您可以跳过所有有错误的 URL,因此您可以简单地捕获 IOError 异常并继续主 for 循环的下一次迭代:

#!/usr/bin/python

import urllib
import cv2
import numpy as np
import os
import httplib

neg_images_link = 'http://image-net.org/api/text/imagenet.synset.geturls?wnid=n03844673'
neg_image_urls = urllib.urlopen(neg_images_link).read().decode()
pic_num = 1


for i in neg_image_urls.split('\n'):
try:
print(i)

try:
r = urllib.urlopen(i).getcode()
except IOError:
print "error opening " + str(i)
continue

print r
if r == 200:
urllib.urlretrieve(i, str(pic_num)+".jpg")
img = cv2.imread(str(pic_num)+".jpg",cv2.IMREAD_GRAYSCALE)
# should be larger than samples / pos pic (so we can place our image on it)
if img != None:
resized_image = cv2.resize(img, (100, 100))
cv2.imwrite(str(pic_num)+".jpg",resized_image)
pic_num += 1

except KeyboardInterrupt:
print "Closing"
break

关于python - 使用Python通过urllib下载图片并测试网站是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40251150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com