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.
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?
Use Python urllib Library To Get Redirection URL.request module. Define a web page URL, suppose this URL will be redirected when you send a request to it. Get the response object. Get the webserver returned response status code, if the code is 301 then it means the URL has been redirected permanently.
The HyperText Transfer Protocol (HTTP) 302 Found redirect status response code indicates that the resource requested has been temporarily moved to the URL given by the Location header.
A 301 redirect is a server-side redirect which redirects users from URL A to URL B, while signaling to search engines that URL A's content has been permanently moved to URL B.
There are five types of redirects: 301, 302, 303, 307, and 308.
requests
handles redirects for you, see redirection and history.
Set allow_redirects=False
if you don't want requests
to handle redirections, or you can inspect the redirection responses contained in the r.history
list.
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_redirects
is True
, the redirects have been followed and the final response returned is the final page after following redirects. If allow_redirects
is False
, the first response is returned, even if it is a redirect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With