【python2.7】urllib2异常处理之urllib2.U

2017-11-05  本文已影响0人  tonyemail_st

参考官方文档:https://docs.python.org/2/howto/urllib2.html#urllib-howto

>>> from urllib2 import URLError
>>> req = urllib2.Request('http://www.pretend_server.org')
>>> try: urllib2.urlopen(req)
except URLError as e:
    print e.reason

[Errno 11004] getaddrinfo failed
>>> req = urllib2.Request('http://www.python.org/fish.html')
>>> try:
    urllib2.urlopen(req)
except urllib2.HTTPError as e:
    print e.code
    print e.read(100)
    print e.reason

    
404
<!doctype html>
<!--[if lt IE 7]>   <html class="no-js ie6 lt-ie7 lt-ie8 lt-ie9">   <![endif]-->
<!-
OK
上一篇 下一篇

猜你喜欢

热点阅读