Arn*_*rnb 5 python get http python-requests
在使用 python requests 库时,我使用 get 和 head http 方法得到了两种不同的响应 -
使用“head”输入:
requests.head("http://stackoverflow.com")
Run Code Online (Sandbox Code Playgroud)
输出:
<Response [301]>
Run Code Online (Sandbox Code Playgroud)
使用“get”输入时:
requests.get("http://stackoverflow.com")
Run Code Online (Sandbox Code Playgroud)
输出是:
<Response [200]>
Run Code Online (Sandbox Code Playgroud)
虽然很明显“ http://stackoverflow.com ”重定向到“ https://stackoverflow.com ”(作为requests.head("https://stackoverflow.com")
返回<Response [200]>
),这解释了第一个实例中的 301 响应,但为什么它没有给出相同的结果“get”方法的响应?
get 和 head 如何以不同的方式工作以产生这两个不同的结果?
我阅读了 w3.org 文档和 stackoverflow 中的类似问题(例如,HEAD 请求收到“403禁止”而 GET“200 ok”?)和其他网站,但他们没有解决这个特殊的差异。
requests.get()
为了您的方便,自动遵循重定向。
如果您不希望出现这种行为,请将其关闭。
resp = requests.get("http://stackoverflow.com", allow_redirects=False)
print(resp.status_code)
Run Code Online (Sandbox Code Playgroud)