【requests】重定向

python requests --> Redirection and History

默认情况下,请求将对除 HEAD 之外的所有动作执行位置重定向。使用 historyResponse 方法来跟踪重定向。

该Response.history列表包含 Response为完成请求而创建的对象。该列表按从最早到最近的响应排序。

例如,GitHub 将所有 HTTP 请求重定向到 HTTPS:

r = requests.get('http://github.com/')

r.url
'https://github.com/'

r.status_code
200

r.history
[<Response [301]>]

如果您使用 GET、OPTIONS、POST、PUT、PATCH 或 DELETE,您可以使用以下参数禁用重定向处理 allow_redirects

r = requests.get('http://github.com/', allow_redirects=False)

r.status_code
301

r.history
[]

如果您使用的是 HEAD,您也可以启用重定向:

r = requests.head('http://github.com/', allow_redirects=True)

r.url
'https://github.com/'

r.history
[<Response [301]>]

猜你喜欢

转载自blog.csdn.net/lan_yangbi/article/details/129162678