This article introduces how to solve the 302 redirection problem when using requests in Python. when using requests to simulate logon, directly use request. get (url) may cause 302 redirection. The reason is that the cookie is not persistent (requests includes cookie processing, but not persistent ),
The solution is to use the request. Session () object to persist the cookie and other states.
For example:
S = requests. Session () response = s. get (url, headers = headers) # Other operations
In addition, a 302 redirection occurs when the Session object is used to request the page without passing in the corresponding request header (for example, Referer), causing the server to return if the verification fails.
Redirect packets. The solution is to construct the corresponding request header, which can be passed in when the page is obtained.
The above is the details about how to solve the 302 redirection problem when using requests in Python. For more information, see other related articles in the first PHP community!