python学习笔记之异常处理(十六)

异常代码如下

>>> import urllib.request
>>> import urllib.error
>>> req = urllib.request.Request("http://www.ooxx-baidu.com")
>>> try:
	urllib.request.urlopen(req)
except urllib.error.URLError as e:
	print(e.reason)

	
[Errno 11001] getaddrinfo failed

404错误

>>> req = urllib.request.Request("http://www.fishC.com/ooxx.html")
>>> try:
	urllib.request.urlopen(req)

except urllib.error.HTTPError as e:
	print(e.code)
	print(e.read())

	
404
b'\r\n404 Not Found\r\n\r\n

404 Not Found

\r\n
nginx
\r\n\r\n\r\n'

你可能感兴趣的:(python)