第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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”部分時出現(xiàn)錯誤,則 urlerror 將不起作用,我會收到一條錯誤消息,指出 ssl.CertificateError。例如target = "https://www.rolcruise.c.uk"我不明白為什么 URLError 沒有涵蓋在 url 某處有拼寫錯誤的所有場景?鑒于它正在發(fā)生,處理 ssl.CertificateError 的下一步是什么?謝謝你的幫助!
查看完整描述

1 回答

?
萬千封印

TA貢獻1891條經(jīng)驗 獲得超3個贊

將 ssl 導(dǎo)入您的命名空間以開始:


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)


查看完整回答
反對 回復(fù) 2021-10-05
  • 1 回答
  • 0 關(guān)注
  • 259 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號