Python-解决URLError
URLError#
import urllib.request
import urllib.parse
import urllib.error
url='http://www.maodan.com/'#没有这个域名的情况下,链接服务器不成功#
try:
response=urllib.request.urlopen(url)
print(response)
except Exception as e:#Exception可以精确捕获任何的异常#
print(e)
———————————————————————————————————————
URLError#
import urllib.request
import urllib.parse
import urllib.error
url='http://www.maodan.com/'#没有这个域名的情况下#
try:
response=urllib.request.urlopen(url)
print(response)
except urllib.error.URLError as e:#urllib.error.URLError也可以精确捕获任何的异常#
print(e)
———————————————————————————————————————
URLError#
HTTPError#
import urllib.request
import urllib.parse
import urllib.error
url='https://bbs.csdn.net/topics/39231478'#URL输入性错误#
try:
response=urllib.request.urlopen(url)
print(response)
except urllib.error.HTTPError as e:#HTTPError属于URLError的子项#
print(e)
print(e.code)#HTTP的状态码#
except urllib.error.URLError as e:
print(e)