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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

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 的某些部分時(shí)出錯(cuò),urlerror 處理工作正常,即如果我故意輸入“htps”而不是“https”,或“ww”而不是“www”,或“u”而不是“英國”。例如target = "https://www.rolcruise.co.u"但是,如果在輸入主機(jī)名(“rolcruise”)或 url 的“co”部分時(shí)出現(xiàn)錯(cuò)誤,則 urlerror 將不起作用,我會(huì)收到一條錯(cuò)誤消息,指出 ssl.CertificateError。例如target = "https://www.rolcruise.c.uk"我不明白為什么 URLError 沒有涵蓋在 url 某處有拼寫錯(cuò)誤的所有場(chǎng)景?鑒于它正在發(fā)生,處理 ssl.CertificateError 的下一步是什么?謝謝你的幫助!
查看完整描述

1 回答

?
萬千封印

TA貢獻(xiàn)1891條經(jīng)驗(yàn) 獲得超3個(gè)贊

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


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

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