python network_error ai_generated true

aiohttp客户端异常:404,消息='未找到',url=URL('http://example.com/nonexistent')

aiohttp.client_exceptions.ClientResponseError: 404, message='Not Found', url=URL('http://example.com/nonexistent')

ID: python/aiohttp-client-http-error

其他格式: JSON · Markdown 中文 · English
80%修复率
86%置信度
0证据数
2025-06-01首次发现

版本兼容性

版本状态引入弃用备注
3.7 active
3.8 active

根因分析

服务器返回4xx或5xx状态码,如果调用raise_for_status,aiohttp会引发异常。

English

The server returns a 4xx or 5xx status code, and aiohttp raises an exception if raise_for_status is called.

generic

解决方案

  1. 95% 成功率
    if resp.status != 200: raise Exception(f'HTTP {resp.status}')
  2. 90% 成功率
    try: resp.raise_for_status() except aiohttp.ClientResponseError as e: print(e.status)

无效尝试

常见但无效的做法:

  1. 60% 失败

    The error is raised by raise_for_status(); if you don't call it, no exception occurs.

  2. 80% 失败

    The resource doesn't exist; changing method won't help.