为了账号安全,请及时绑定邮箱和手机立即绑定

urlerror 和 ssl.CertificateError

urlerror 和 ssl.CertificateError

尚方宝剑之说 2021-10-05 16:56:56
我有以下代码:from urllib.request import urlopenfrom urllib.error import HTTPError, URLErrorfrom 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 的下一步是什么?谢谢你的帮助!
查看完整描述

1 回答

?
万千封印

TA贡献1891条经验 获得超3个赞

将 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)


查看完整回答
反对 回复 2021-10-05
  • 1 回答
  • 0 关注
  • 242 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信