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

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

ConnectionError 后如何獲取請求的 URL?

ConnectionError 后如何獲取請求的 URL?

慕尼黑5688855 2022-06-22 20:41:17
我最近一直在嘗試制作一個程序,該程序使用 Python Requests 庫返回縮短的 URL(例如 bit.ly 和 t.co URL)導致的 URL。我已經(jīng)能夠使用這種方法通過工作 URL 輕松地做到這一點:reveal = requests.get(shortenedUrl, timeout=5)fullUrl = reveal.url但是,當縮短的 URL 指向不真實的 URL 時(例如:http ://thisurldoesnotexistyet.com/ ),上述方法會按預期返回 ConnectionError。ConnectionError 返回: HTTPSConnectionPool(host='thisurldoesnotexistyet.com', port=443): Max retries exceeded with url: / (Caused by ConnectTimeoutError(<urllib3.connection.VerifiedHTTPSConnection object at 0x00000213DC97F588>, 'Connection to thisurldoesnotexistyet.com timed out. (connect timeout=5)'))發(fā)生這種情況時,我嘗試了這種方法來獲取重定向 URL:try:    reveal = requests.get(shortenedUrl, timeout=5)    fullUrl = reveal.urlexcept requests.exceptions.ConnectionError as error:    fullUrl = "http://" + error.host但是,該方法不起作用(AttributeError: 'ConnectTimeout' object has no attribute 'host')。有什么方法可以讓我從錯誤中獲取縮短的 URL 重定向到的 URL?Python蟒蛇請求http請求
查看完整描述

1 回答

?
米脂

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

您正在請求一個不存在的 url。因此,您會超時。


>>> requests.get('https://does-not-exist')

... (suppressed for clarity)

requests.packages.urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='does-not-exist', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f6b6dba7210>: Failed to establish a new connection: [Errno -2] Name or service not known'))

主機是您傳入的url。您可以捕獲異常并查看您傳入的相同 url,但您將 url 傳遞給requests.get.


>>> try:

...     requests.get('https://does-not-exist')

... except requests.exceptions.ConnectionError as error:

...     print(error.request.url)

...

https://does-not-exist/


查看完整回答
反對 回復 2022-06-22
  • 1 回答
  • 0 關注
  • 227 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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