如何在 ConnectionError 之后获取请求的 URL?

How can I get the URL of a request after a ConnectionError?

我最近一直在尝试制作一个程序,returns URL 缩短了 URL(例如 bit.ly 和 t.co URLs) 导致使用 Python Requests 库。我已经能够通过使用此方法工作 URL 轻松地做到这一点:

reveal = requests.get(shortenedUrl, timeout=5)
fullUrl = reveal.url

但是,当缩短的 URL 指向一个不真实的 URL(例如:http://thisurldoesnotexistyet.com/)时,上面的方法 returns 会按预期出现 ConnectionError。 ConnectionError returns 这个: 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)'))

发生这种情况时,我尝试了这种方法来获取重定向 URL:

try:
    reveal = requests.get(shortenedUrl, timeout=5)
    fullUrl = reveal.url
except requests.exceptions.ConnectionError as error:
    fullUrl = "http://" + error.host

但是,该方法不起作用 (AttributeError: 'ConnectTimeout' object has no attribute 'host')。有什么方法可以让我从错误中得到缩短的 URL 重定向到的 URL 吗?

您请求的 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'))

host就是你传入的url,你可以catch异常看到和你传入的一样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/