[python] 2026-08-20 圈点403
摘要:Requests重定向问题分析,request打开网址后自动跳转问题的分析
Requests重定向问题分析,request打开网址后自动跳转问题的分析。
默认情况下,除了 HEAD, Requests 会自动处理所有重定向。
可以使用响应对象的 history 方法来追踪重定向。
如果使用的是GET、OPTIONS、POST、PUT、PATCH 或者 DELETE,那么可以通过 allow_redirects 参数禁用重定向处理:
示例重定向的情况:
禁用重定向
response = requests.get('http://a.com',allow_redirects=False)
print(response.url) # 'https://a.com/'
print(response.status_code) # 300
print(response.history) # []使用重定向(默认值)
response = requests.head('http://a.com',allow_redirects=True)
print(response.url) # 'https://a.com/'
print(response.status_code) # 200
print(response.history) # [<Response [301]>]分析 :示例中的网址 http://a.com 默认会跳转到https://a.com域名下,如果禁用重定向,所以会显示300错误,使用重定向时,其会自动跳转到https,显示301跳转