# aiohttp.client_exceptions.InvalidURL: URL 应为绝对地址

- **ID:** `python/aiohttp-invalid-url-with-scheme`
- **领域:** python
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

aiohttp 要求完全限定的 URL，必须包含协议和主机；相对路径或缺少 http(s):// 的字符串会被拒绝。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   Use urllib.parse.urljoin to build an absolute URL:

from urllib.parse import urljoin
url = urljoin('https://api.example.com', '/v1/users')
async with session.get(url) as resp:
    ...
   ```
2. **** (90% 成功率)
   ```
   Store full URLs in your config and validate at startup:

assert url.startswith(('http://', 'https://')), f'bad url: {url}'
   ```

## 无效尝试

- **** — base_url only affects redirects and links; session.get('/path') still raises InvalidURL. (70% 失败率)
- **** — Encoding does not add the missing scheme/host; the URL remains relative. (80% 失败率)
