Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
下载python中带有URL的图像:错误_Python_Url - Fatal编程技术网

下载python中带有URL的图像:错误

下载python中带有URL的图像:错误,python,url,Python,Url,我试图用python下载带有URL的图像,但不幸的是,我遇到了一个错误,我不知道如何处理它 我正在使用以下代码: def dowload(url_img): try : r = requests.get(url_img) with open('a.jpg', 'wb') as f: f.write(r.content) except urllib2.HTTPError: print("Some Err

我试图用python下载带有URL的图像,但不幸的是,我遇到了一个错误,我不知道如何处理它

我正在使用以下代码:

def dowload(url_img):
    try :
        r = requests.get(url_img)
        with open('a.jpg', 'wb') as f:
           f.write(r.content)
    except urllib2.HTTPError:
        print("Some Err")
    if os.path.exists("a.jpg"):
       os.remove("a.jpg")
    return img
图像的URL为:”http://cloudimages.youthconnect.in/wp-content/uploads/2015/07/539.jpg"

问题是我得到了以下错误:

---------------------------------------------------------------------------
gaierror                                  Traceback (most recent call last)
/usr/lib/python3.6/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
   1324                 h.request(req.get_method(), req.selector, req.data, headers,
-> 1325                           encode_chunked=req.has_header('Transfer-encoding'))
   1326             except OSError as err: # timeout error

16 frames
gaierror: [Errno -2] Name or service not known

During handling of the above exception, another exception occurred:

URLError                                  Traceback (most recent call last)
/usr/lib/python3.6/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
   1325                           encode_chunked=req.has_header('Transfer-encoding'))
   1326             except OSError as err: # timeout error
-> 1327                 raise URLError(err)
   1328             r = h.getresponse()
   1329         except:

URLError: <urlopen error [Errno -2] Name or service not known>
---------------------------------------------------------------------------
GAIRROR回溯(最近一次呼叫最后一次)
/dou open中的usr/lib/python3.6/urllib/request.py(self,http_类,req,**http_conn_参数)
1324 h.请求(请求获取方法()、请求选择器、请求数据、标题、,
->1325 encode_chunked=req.has_头('Transfer-encoding'))
1326除OSError as err外:#超时错误
16帧
gaierror:[Errno-2]名称或服务未知
在处理上述异常期间,发生了另一个异常:
URLError回溯(最近一次调用上次)
/dou open中的usr/lib/python3.6/urllib/request.py(self,http_类,req,**http_conn_参数)
1325 encode_chunked=req.has_头('Transfer-encoding'))
1326除OSError as err外:#超时错误
->1327错误(err)
1328 r=h.getresponse()
1329除了:
URL错误:
我无法检查是否获得状态代码200,因为无法访问该站点。当我试图打开连接,然后在那个时候我得到这个错误


如果有人能为这个问题提供一些解决办法,我将不胜感激。url来自VGFace数据集,我正在尝试逐个下载图像。

好的,我使用简单的
解决了它,除了:
语句,因为我不需要知道异常的类型。

好的,我通过使用简单的
语句解决了这个问题,除了:
语句,因为我不需要知道异常的类型。

URL是否正确?@IainShelvington URL来自VGG face数据集,所以我必须逐个查看这些URL,检查它们是否有效&如果有效,然后下载图像。所以我不知道如何检查这样的URL。通常我会检查响应或状态代码,但这里的情况有所不同。您能否捕获
URLError
以处理URL无效或站点不再存在的情况?DNS查找对此不返回任何结果domain@IainShelvington如果我能捕捉到这样的异常,那么我将跳过该图像&什么也不返回。正如您所见,该函数以URL作为参数,因此我有很多URL。您已经捕获了
urllib2.HTTPError
,除了块之外,你不能再添加一个
块吗?URL正确吗?@IainShelvington该URL来自VGG face数据集,因此我必须逐一查看这些URL,检查它们是否有效&如果有效,然后下载图像。所以我不知道如何检查这样的URL。通常我会检查响应或状态代码,但这里的情况有所不同。您能否捕获
URLError
以处理URL无效或站点不再存在的情况?DNS查找对此不返回任何结果domain@IainShelvington如果我能捕捉到这样的异常,那么我将跳过该图像&什么也不返回。正如您所看到的,该函数以URL作为参数,因此我有很多URL。您已经在捕获
urllib2.HTTPError
,您不能添加除
块之外的其他