目录
urllib.error
案例1 :
案例2:
(用 request.openurl() 都应该放在 try 中)
'''
URLError的使用
'''
from urllib import request, error
if __name__ == '__main__':
url = "http://www.baiiiiiiiiiidu.com"
try:
req = request.Request(url)
rsp = request.urlopen( req )
html = rsp.read().decode()
print(html)
except error.URLError as e:
print("URLError: {0}".format(e.reason))
print("URLError: {0}".format(e))
except Exception as e:
print(e)
'''
URLError的使用
'''
from urllib import request, error
if __name__ == '__main__':
url = "http:iiiiiiiiidu//www.baidu.com/welcome.html"
url = "http://www.sipo.gov.cn/www"
try:
req = request.Request(url)
rsp = request.urlopen( req )
html = rsp.read().decode()
print(html)
except error.HTTPError as e:
print("HTTPError: {0}".format(e.reason))
print("HTTPError: {0}".format(e))
except error.URLError as e:
print("URLError: {0}".format(e.reason))
print("URLError: {0}".format(e))
except Exception as e:
print(e)
'''
URLError的使用
'''
from urllib import request, error
if __name__ == '__main__':
#一个不存在的连接
url = "http://www.baidu.com/oooooo"
try:
req = request.Request(url)
rsp = request.urlopen( req )
html = rsp.read().decode()
print(html)
except error.URLError as e:
if hasattr(e, 'code')
print("HTTPError")
print(e.code)
elif hasattr(e, 'reason')
print("URLError")
print(e.reason)