python请求中的Http重定向代码3XX

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22150023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:22:36  来源:igfitidea点击:

Http Redirection code 3XX in python requests

pythonredirectpython-requestshttp-status-codes

提问by Bishwash

I am trying to capture http status code 3XX/302 for a redirection url. But I cannot get it because it gives 200 status code.

我正在尝试为重定向 url 捕获 http 状态代码 3XX/302。但我无法得到它,因为它给出了 200 状态代码。

Here is the code:

这是代码:

import requests
r = requests.get('http://goo.gl/NZek5')
print r.status_code

I suppose this should issue either 301 or 302 because it redirects to another page. I had tried few redirecting urls (for e.g. http://fb.com) but again it is issuing the 200. What should be done to capture the redirection code properly?

我想这应该发出 301 或 302,因为它重定向到另一个页面。我尝试了几个重定向 url(例如http://fb.com),但它再次发出 200。应该怎么做才能正确捕获重定向代码?

采纳答案by Martijn Pieters

requestshandles redirects for you, see redirection and history.

requests为您处理重定向,请参阅重定向和历史记录

Set allow_redirects=Falseif you don't want requeststo handle redirections, or you can inspect the redirection responses contained in the r.historylist.

设置allow_redirects=False您是否不想requests处理重定向,或者您可以检查包含在r.historylist 中的重定向响应。

Demo:

演示:

>>> import requests
>>> url = 'https://httpbin.org/redirect-to'
>>> params = {"status_code": 301, "url": "https://stackoverflow.com/q/22150023"}
>>> r = requests.get(url, params=params)
>>> r.history
[<Response [301]>, <Response [302]>]
>>> r.history[0].status_code
301
>>> r.history[0].headers['Location']
'https://stackoverflow.com/q/22150023'
>>> r.url
'https://stackoverflow.com/questions/22150023/http-redirection-code-3xx-in-python-requests'
>>> r = requests.get(url, params=params, allow_redirects=False)
>>> r.status_code
301
>>> r.url
'https://httpbin.org/redirect-to?status_code=301&url=https%3A%2F%2Fstackoverflow.com%2Fq%2F22150023'

So if allow_redirectsis True, the redirects have been followed and the final response returned is the final page after following redirects. If allow_redirectsis False, the first response is returned, even if it is a redirect.

因此,如果allow_redirectsTrue,则已遵循重定向,并且返回的最终响应是遵循重定向后的最终页面。如果allow_redirectsFalse,则返回第一个响应,即使它是重定向。

回答by George Bahij

requests.getallows for an optional keyword argument allow_redirectswhich defaults to True. Setting allow_redirectsto Falsewill disable automatically following redirects, as follows:

requests.get允许一个可选的关键字参数allow_redirects,默认为True. 设置allow_redirectsFalse将禁用自动跟随重定向,如下所示:

In [1]: import requests
In [2]: r = requests.get('http://goo.gl/NZek5', allow_redirects=False)
In [3]: print r.status_code
301

回答by Wes

This solution will identify the redirect and display the history of redirects, and it will handle common errors. This will ask you for your URL in the console.

此解决方案将识别重定向并显示重定向的历史记录,并将处理常见错误。这将在控制台中询问您的 URL。

import requests

def init():
    console = input("Type the URL: ")
    get_status_code_from_request_url(console)


def get_status_code_from_request_url(url, do_restart=True):
    try:
        r = requests.get(url)
        if len(r.history) < 1:
            print("Status Code: " + str(r.status_code))
        else:
            print("Status Code: 301. Below are the redirects")
            h = r.history
            i = 0
            for resp in h:
                print("  " + str(i) + " - URL " + resp.url + " \n")
                i += 1
        if do_restart:
            init()
    except requests.exceptions.MissingSchema:
        print("You forgot the protocol. http://, https://, ftp://")
    except requests.exceptions.ConnectionError:
        print("Sorry, but I couldn't connect. There was a connection problem.")
    except requests.exceptions.Timeout:
        print("Sorry, but I couldn't connect. I timed out.")
    except requests.exceptions.TooManyRedirects:
        print("There were too many redirects.  I can't count that high.")


init()