Home > Backend Development > Python Tutorial > How to solve the 302 redirection problem when using requests in Python

How to solve the 302 redirection problem when using requests in Python

高洛峰
Release: 2017-03-30 13:25:24
Original
9323 people have browsed it

When using requests to simulate login, using request.get(url) directly can easily cause 302 redirection. The reason is that cookies are not persistent (requests comes with cookie processing, but it is not persistent).

The solution is to use the request.Session() object to persist cookie and other states.

For example:

 s = requests.Session()
 response = s.get(url, headers=headers)
 
 #其他操作
Copy after login

Another situation that will cause 302 redirection is: when using the Session object to request the page, the corresponding request header (for example: Referer) is not passed in, causing the server verification to fail. Return

redirect message. The solution is to construct the corresponding request header and pass it in when fetching the page.

The above is the detailed content of How to solve the 302 redirection problem when using requests in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template