对不存在的网页抓取并处理异常

import urllib.request
import urllib.error

myURL1 = urllib.request.urlopen("https://www.runoob.com/")
print(myURL1.getcode())   # 200

try:
    myURL2 = urllib.request.urlopen("https://www.runoob.com/no.html")
except urllib.error.HTTPError as e:
    if e.code == 404:
        print(404)   # 404

posted @ 2022-02-14 22:45  xjspyx  阅读(18)  评论(0编辑  收藏  举报