# aiohttp.client_exceptions.ClientResponseError: 429, message='请求过于频繁', url=URL('https://api.example.com/v1/items')

- **ID:** `python/aiohttp-client-response-error`
- **领域:** python
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

远程服务器返回了 4xx/5xx 状态码，且会话/请求配置了 raise_for_status=True，因此 aiohttp 抛出 ClientResponseError 而非返回响应。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |
| 3.10 | active | — | — |
| 3.11 | active | — | — |
| 3.12 | active | — | — |

## 解决方案

1. **** (88% 成功率)
   ```
   import asyncio, random
for attempt in range(5):
    async with session.get(url) as r:
        if r.status == 429:
            await asyncio.sleep((2 ** attempt) + random.random())
            continue
        r.raise_for_status()
        return await r.json()
   ```
2. **** (90% 成功率)
   ```
   sem = asyncio.Semaphore(5)
async def fetch(u):
    async with sem:
        async with session.get(u) as r:
            r.raise_for_status()
            return await r.json()
   ```

## 无效尝试

- **** — Hides 5xx and auth failures; downstream code receives HTML error pages parsed as JSON. (75% 失败率)
- **** — Aggravates rate limiting and often escalates to a 403 block. (85% 失败率)
