捕获ValueError:未知的url类型:urllib2 Python

时间:2014-12-01 23:29:18

标签: python

当我输入无效网址时,我收到以下异常:ValueError: unknown url type: blob

我尝试使用以下代码捕获此错误,但它无效:

try:
    req = urllib2.Request(url)
    conn =  urllib2.urlopen(req)
    content = conn.read()
except urllib2.URLError, e:
    print 'Error: Invalid URL'

1 个答案:

答案 0 :(得分:2)

如果您注意到,基本错误是ValueError,那么

import urllib2
url = 'blob'
try:
    req = urllib2.Request(url)
    conn =  urllib2.urlopen(req)
    content = conn.read()
except ValueError, e:
    print 'Error: Invalid URL'