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

高洛峰
高洛峰Original
2017-03-30 13:25:249354browse

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)
 
 #其他操作

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!

Statement:
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