为了账号安全,请及时绑定邮箱和手机立即绑定

如何检查 urllib.urlretrieve 是否能够使用 python 访问主机?

如何检查 urllib.urlretrieve 是否能够使用 python 访问主机?

翻过高山走不出你 2021-05-30 12:32:19
我正在尝试从imgnet链接中收集图像并将其保存在文件夹中。使用urllib.urlretrieve,直到知道它工作得很好。 主要问题是当函数试图访问 DNS 无法解析的主机时。 我的链接是:http : //www.fuminnaosu.com/image/person/kitamura.jpg当每个 DNS 都无法解析时(可能会被删除),但它仍然在列表中。Try&Except 目前无法在我的项目中使用,有什么方法可以在不使用 Try&Except 的情况下通过并继续收集?我的错误是:  File "C:\Anaconda2\lib\socket.py", line 557, in create_connectionfor res in getaddrinfo(host, port, 0, SOCK_STREAM):IOError: [Errno socket error] [Errno 11001] getaddrinfo failed我的代码:import cv2import numpy as npimport osimport urllibdef get_images_to_dir():    images_list_lnk = 'http://image-net.org/api/text/imagenet.synset.geturls?wnid=n09618957'    images_url = urllib.urlopen(images_list_lnk)    urls = images_url.read()    img_typ = 'neg'    if not os.path.exists(img_typ):        os.mkdir(img_typ)    images_counter = 1    for url in urls.split('\n'):            img_name = img_typ+'/'+str(images_counter)+'.jpg'            urllib.urlretrieve(url, img_name)            img = cv2.imread(img_name, cv2.IMREAD_GRAYSCALE)            if (img is not None): # If connection success, but no image was found                img = cv2.resize(img, (100, 100))                cv2.imwrite(img_name, img)                images_counter += 1
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 183 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号