URL检查的异常处理无效

2024-04-26 10:54:26 发布

您现在位置:Python中文网/ 问答频道 /正文

当我在浏览器中手动尝试URL时,我看到下面的错误 enter image description here

我正在尝试使用异常处理捕获代码中的错误:

with requests.session() as session:

try:
    f.write('URL 1\n')
    page = session.get(url=url)
    page.raise_for_status()  # If successful, no Exception

except HTTPError as e:
    f.write('*****************Attention*****************\n')
    f.write(f'HTTP error occurred: {e}\n')
    f.write('*******************************************\n\n\n')

但是,我的代码无法捕获故障,而是在执行代码时,我看到以下响应:

    Traceback (most recent call last):
  File "C:\Program Files\Python38\lib\site-packages\urllib3\connection.py", line 159, in _new_conn
    conn = connection.create_connection(
  File "C:\Program Files\Python38\lib\site-packages\urllib3\util\connection.py", line 84, in create_connection
    raise err
  File "C:\Program Files\Python38\lib\site-packages\urllib3\util\connection.py", line 74, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\Python38\lib\site-packages\urllib3\connectionpool.py", line 670, in urlopen
    httplib_response = self._make_request(
  File "C:\Program Files\Python38\lib\site-packages\urllib3\connectionpool.py", line 392, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "C:\Program Files\Python38\lib\http\client.py", line 1240, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Program Files\Python38\lib\http\client.py", line 1286, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Program Files\Python38\lib\http\client.py", line 1235, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Program Files\Python38\lib\http\client.py", line 1006, in _send_output
    self.send(msg)
  File "C:\Program Files\Python38\lib\http\client.py", line 946, in send
    self.connect()
  File "C:\Program Files\Python38\lib\site-packages\urllib3\connection.py", line 187, in connect
    conn = self._new_conn()
  File "C:\Program Files\Python38\lib\site-packages\urllib3\connection.py", line 171, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x000001A89D8155B0>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it

Tags: inpyselfrequestlibpackageslinesite
1条回答
网友
1楼 · 发布于 2024-04-26 10:54:26

您正在处理错误类型的异常,您正在捕获HTTPError,但是ConnectionRefusedError被抛出,并且ConnectionRefusedError不是HTTPError(通过继承)

如果您期望其中一个,请同时捕获两个:

try:
    # your code
except (HTTPError, ConnectionRefusedError):
    # deal with it

如果您需要以不同方式处理它们,请分别捕获它们:

try:
    # your code
except HTTPError as e:
    # deal with http error
except ConnectionRefusedError:
    # deal with connection refused error

请注意,as e位将允许您引用异常(在本例中为e),这将允许您找出错误或告诉用户

相关问题 更多 >