python network_error ai_generated true

requests.exceptions.TooManyRedirects: 在获取'https://redirect.example.com'时超过30次重定向

requests.exceptions.TooManyRedirects: Exceeded 30 redirects while fetching 'https://redirect.example.com'

ID: python/requests-connectionerror-too-many-redirects

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

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

服务器在循环中重定向客户端或重定向次数过多。

English

The server is redirecting the client in a loop or too many times.

generic

解决方案

  1. 85% 成功率 Manually handle redirects by inspecting the Location header
    response = requests.get('https://redirect.example.com', allow_redirects=False)
    while response.is_redirect:
        next_url = response.headers['Location']
        response = requests.get(next_url, allow_redirects=False)
        if response.status_code == 200:
            break
  2. 80% 成功率 Use a session with a custom redirect handler to detect loops
    from requests import Session
    s = Session()
    s.max_redirects = 10
    response = s.get('https://redirect.example.com')

无效尝试

常见但无效的做法:

  1. Disabling redirects entirely 70% 失败

    May miss the final target if redirects are legitimate.

  2. Increasing the maximum redirect limit arbitrarily 80% 失败

    If it's a loop, increasing limit does not help; it will still exceed.