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

Python如何使用Requests库获取URL重定向后的目标地址

问题排查与解决方案

原因说明

请求返回503状态码为目标站点反爬机制拦截导致,仅携带User-Agent的请求头不符合浏览器完整请求特征,被站点防护规则拦截。浏览器发起请求时会自动补全所有标准请求头、处理Cookie与人机校验逻辑,因此可以正常跳转。

解决方案1:补全请求头使用原生Requests

补全浏览器标准请求头,使用Session会话自动处理Cookie:

import requests

url = "https://zephr.app/pu?u=db3f63382398497c8d6a52b06cd7a880"
headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
    "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
    "Accept-Encoding": "gzip, deflate, br",
    "Connection": "keep-alive",
    "Upgrade-Insecure-Requests": "1",
    "Sec-Fetch-Dest": "document",
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-Site": "none",
    "Sec-Fetch-User": "?1"
}

session = requests.Session()
response = session.get(url, headers=headers, timeout=10)

print("重定向最终地址:", response.url)
print("跳转历史:")
for res in response.history:
    print(res.status_code, res.url)

如果该方案仍返回503,说明站点开启了Cloudflare 5秒人机校验,原生Requests无法绕过,使用方案2。

解决方案2:使用cloudscraper绕过Cloudflare校验

cloudscraper是专门适配Cloudflare防护的请求库,使用逻辑和Requests完全一致。
首先安装依赖:
pip install cloudscraper
使用示例:

import cloudscraper

url = "https://zephr.app/pu?u=db3f63382398497c8d6a52b06cd7a880"
scraper = cloudscraper.create_scraper(browser={
    'browser': 'chrome',
    'platform': 'macos',
    'mobile': False
})
response = scraper.get(url, timeout=10)

print("重定向最终地址:", response.url)
print("跳转历史:")
for res in response.history:
    print(res.status_code, res.url)

排查建议

  • 保证请求的网络环境和正常访问的浏览器网络一致,避免使用被站点拉黑的代理、VPN节点
  • 不要短时间内高频请求该链接,避免触发站点频率限制规则
  • 可以打印response.text查看503返回的页面内容,确认具体的拦截原因

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 21:06:03