urlerror 和 ssl.CertificateError

我有以下代码:


from urllib.request import urlopen

from urllib.error import HTTPError, URLError

from bs4 import BeautifulSoup


# target = "https://www.rolcruise.co.uk/cruise-detail/1158731-hawaii-round-trip-honolulu-2020-05-23"

target = "https://www.rolcruise.co.uk"


try:

    html = urlopen(target)

except HTTPError as e:

    print("You got a HTTP Error. Something wrong with the path.")

    print("Here is the error code: " + str(e.code))

    print("Here is the error reason: " + e.reason)

    print("Happy for the program to end here"

except URLError as e:

    print("You got a URL Error. Something wrong with the URL.")

    print("Here is the error reason: " + str(e.reason))

    print("Happy for the program to end here")

else:

    bs_obj = BeautifulSoup(html, features="lxml")

    print(bs_obj)

如果我故意在输入 url 的某些部分时出错,urlerror 处理工作正常,即如果我故意输入“htps”而不是“https”,或“ww”而不是“www”,或“u”而不是“英国”。例如


target = "https://www.rolcruise.co.u"

但是,如果在输入主机名(“rolcruise”)或 url 的“co”部分时出现错误,则 urlerror 将不起作用,我会收到一条错误消息,指出 ssl.CertificateError。例如


target = "https://www.rolcruise.c.uk"

我不明白为什么 URLError 没有涵盖在 url 某处有拼写错误的所有场景?


鉴于它正在发生,处理 ssl.CertificateError 的下一步是什么?


谢谢你的帮助!


尚方宝剑之说
浏览 243回答 1
1回答

万千封印

将 ssl 导入您的命名空间以开始:import ssl然后你可以捕获那种异常:try:    html = urlopen(target)except HTTPError as e:    print("You got a HTTP Error. Something wrong with the path.")    print("Here is the error code: " + str(e.code))    print("Here is the error reason: " + e.reason)    print("Happy for the program to end here"except URLError as e:    print("You got a URL Error. Something wrong with the URL.")    print("Here is the error reason: " + str(e.reason))    print("Happy for the program to end here")except ssl.CertificateError:     # Do your stuff here...else:    bs_obj = BeautifulSoup(html, features="lxml")    print(bs_obj)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python