You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Requests代理请求:HTTPS正常但HTTP仅返回Header问题

Requests代理下HTTP链接无法触发重定向的问题分析与解决

问题现象

  • 使用Python Requests库通过代理请求HTTP协议URL时,无法触发浏览器中正常出现的HTTPS重定向
  • 响应状态码为200,history属性为空,返回内容仅包含请求头元数据(如REMOTE_ADDR、REQUEST_URI等)
  • 改用HTTPS URL或直接访问(无代理)时,重定向正常触发,能获取完整页面源码

核心原因

  1. 代理协议不匹配:当用HTTPS代理处理HTTP请求时,代理服务器可能未正确转发重定向指令,目标服务器识别到代理请求后返回了代理调试信息而非重定向响应
  2. 请求头不符合浏览器规范:请求中设置了Accept: application/json,部分服务器在接收HTTP+代理+该头的组合请求时,会返回元数据而非标准网页重定向
  3. Requests代理配置逻辑:Requests中http字段对应HTTP请求的代理,https字段对应HTTPS请求的代理,配置错位会导致请求处理异常

解决方案

1. 修正代理协议匹配

确保HTTP请求使用HTTP代理,HTTPS请求使用对应代理,统一配置:

proxies = {
    "http": "http://3.21.101.158:3128",  # HTTP请求用HTTP代理
    "https": "https://204.236.176.61:3128"  # HTTPS请求用HTTPS代理
}

若代理同时支持HTTP和HTTPS协议,可简化为:

proxies = {
    "http": "http://your-proxy-ip:port",
    "https": "http://your-proxy-ip:port"
}

2. 调整请求头模拟浏览器行为

移除Accept: application/json,改用浏览器默认的Accept头,让服务器返回正常网页内容:

headers = {
    'User-Agent': ua.random,
    'Accept-Language': 'en-GB,en-US;q=0.9,en;q=0.8',
    'Connection': 'keep-alive',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
}

3. 手动处理重定向(备选方案)

如果代理仍无法触发自动重定向,可关闭自动重定向后手动处理:

# 先发起HTTP请求,不自动重定向
htmlRequest = requests.get("http://link.springer.com/10.1023/A:1012637309336",
                          headers=headers,
                          verify=False,
                          allow_redirects=False,
                          proxies=proxies,
                          timeout=30)

# 检查重定向状态码和Location头
if htmlRequest.status_code in [301, 302] and 'Location' in htmlRequest.headers:
    redirect_url = htmlRequest.headers['Location']
    # 发起HTTPS重定向请求
    htmlRequest = requests.get(redirect_url,
                              headers=headers,
                              verify=False,
                              proxies=proxies,
                              timeout=30)

验证代码

import requests
from bs4 import BeautifulSoup
from fake_useragent import UserAgent

ua = UserAgent(browsers=['Edge', 'Chrome', 'Firefox'], os='Windows', platforms='desktop')
headers = {
    'User-Agent': ua.random,
    'Accept-Language': 'en-GB,en-US;q=0.9,en;q=0.8',
    'Connection': 'keep-alive',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
}

proxies = {
    "http": "http://3.21.101.158:3128",
    "https": "http://3.21.101.158:3128"
}

htmlRequest = requests.get("http://link.springer.com/10.1023/A:1012637309336",
                          headers=headers,
                          verify=False,
                          allow_redirects=True,
                          proxies=proxies,
                          timeout=30)

print(f"状态码: {htmlRequest.status_code}")
print(f"重定向历史: {[resp.url for resp in htmlRequest.history]}")
print(f"最终URL: {htmlRequest.url}\n")
soup = BeautifulSoup(htmlRequest.content, 'html.parser')
print(f"页面标题: {soup.title.text}")  # 验证是否获取正常页面

关键提示

  • 公开代理稳定性差,若问题持续建议更换高质量代理
  • verify=False仅用于测试,生产环境需配置正确的证书验证

内容的提问来源于stack exchange,提问作者Simonhawk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 11:35:09