Usually we get caught in the process, you will see 302 status code, then the process what happened?
What is redirection
Various methods is through various network requests to re-set a direction other locations, but should have a final departure and arrival b from the c a, such a scenario is called redirect
Redirect status
Speaking redirection, certainly missed http status codes
300: The requested resource corresponds to a representation of a collection of some representation, each representation has its own specific position
301: Indicates the resource has permanently moved to a new location, and reference should be new URI future it requests for
302: indicates resource has temporarily moved to another location, but future references should still use the original URI to access the resource. This definition is retained for backward compatibility. SC_FOUND now the preferred definitions
303: indication can be found in response to the request of another under the URI
304: GET operation condition indicating discovery resource is available but can not be modified
305: instructions must be given access to the requested resource through a proxy Location field
307: indication request resource resides temporarily under another URI. Temporary URI should be provided by the Location field in the response
How to deal with redirects
When we encounter such a redirect, how should we deal with?
# request源码中 param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``. :type allow_redirects: bool
Discovery requests default is True, to allow redirection.
# Coding: UTF. 8- Import Requests URL = ' http://github.com ' # redirect False R & lt requests.get = (URL, allow_redirects = False) Print (r.status_code) Print (r.url) Code Results : 301 HTTP: //github.com/
Default turned on, how do we know that the process has not found a request to redirect it?
requests the return of history can help us solve
# coding:utf-8 import requests url = 'http://github.com' # 重定向为True r = requests.get(url,allow_redirects=True) print(r.status_code) print(r.history) 代码结果: 200 [<Response [301]>]
Found that if we allow the redirection status code returned is 200, the request status code by viewing the history and found 301 had intermediate request