【问题标题】:Python requests head shows "301" while get returns "200"Python请求头显示“301”而get返回“200”
【发布时间】:2019-11-15 05:38:54
【问题描述】:

在使用 python 请求库时,我使用 get 和 head http 方法得到了两个不同的响应 -

使用“head”输入:

requests.head("http://stackoverflow.com")

输出:

<Response [301]>

使用“get”输入时:

requests.get("http://stackoverflow.com")

输出是:

<Response [200]>

虽然这很明显“http://stackoverflow.com”重定向到“https://stackoverflow.com”(因为requests.head("https://stackoverflow.com") 返回&lt;Response [200]&gt;),这解释了第一个实例的 301 响应,但为什么它没有给出“get”方法的响应相同?

get 和 head 如何以不同的方式产生这两种不同的结果?

我阅读了 w3.org 文档和 stackoverflow(例如,HEAD request receives "403 forbidden" while GET "200 ok"?)和其他网站中的类似问题,但他们没有解决这个特殊的差异。

【问题讨论】:

    标签: python http get python-requests


    【解决方案1】:

    requests.get() 会自动跟踪重定向,以方便您使用。

    如果您不想要这种行为,请将其关闭。

    resp = requests.get("http://stackoverflow.com", allow_redirects=False)
    print(resp.status_code)
    

    【讨论】:

      猜你喜欢
      • 2020-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-22
      • 2019-05-23
      • 2015-11-14
      • 2019-06-25
      • 1970-01-01
      相关资源
      最近更新 更多