Performing Login with Python requests (Cookies not activated)

China☆狼群 提交于 2020-06-13 07:01:08

问题


I'm trying to perform a login with the python requests module. I first post to the login-page with my user name and password to save the cookie from the response. After that I try to enter the password protected page. The problem is that the response page says I have to activate cookies to login to this page, but I get a Cookie back that I can display. Here is the code

Input parameters from the login page:

<input type="hidden" name="security_token" value="kAvcFvod9KSrGLS1JX30+dhNI8QhmVpbqbe00zuvztE="> 
<input type="hidden" name="login_ticket" value="3781fe1e16e441e9957bec8b32aa4630">
<input type="hidden" name="resolution"  value="">
<input type="hidden" name="device_pixel_ratio" value="1">

and

<input type="text" autofocus id="loginname" name="loginname" value="" size="20" maxlength="63">
<input type="password"  id="password" name="password" size="20">

Python Code:

import requests

login = r'LOGIN_URL'
url = 'PROTECTED_URL'

values = {'loginname': 'USERNAME', 'password': r'PASSWORD','security_token' : r'kAvcFvod9KSrGLS1JX30+dhNI8QhmVpbqbe00zuvztE=', 'login_ticket' : r'3781fe1e16e441e9957bec8b32aa4630', 'resolution' : '', 'device_pixel_ratio' : '1'}

session = requests.Session()

r = session.post(login, data = values)
print r.content
print r.cookies
s = session.get(url)

(I get the security token and login ticket from another get request before) So, the r.content prints out a web page which basically says: Login not successful, because Cookies are not activated.

But the output from the r.cookies is:

<RequestsCookieJar[<Cookie Seminar_Session=abbfdd4e96ee048b1b97fd027a2c7de4 for URL/>]>

And s.get just returns the login page you get if you are not logged in and try to access the protected page.

So I think everything works just fine, just the web page thinks I don't save the sent cookies. How can I work around that?

EDIT (Solution):

Worked, thanks!

Solution with BeautifulSoup:

import requests
from bs4 import BeautifulSoup

login = 'LOGIN_URL'
url = 'PROTECTED_URL'

session = requests.Session()

l = session.get(login) 

soup = BeautifulSoup(l.content,'lxml')

values = {'loginname': 'USERNAME', 'password': r'PASSWORD','security_token' : soup.find('input')['value'], 'login_ticket' : soup.find('input').next_sibling.next_sibling['value'], 'resolution' : '', 'device_pixel_ratio' : '1'}

r = session.post(login, data = values)
s = session.get(url)

回答1:


Try an initial GET of the login page with r = session.get(login). Then check session.cookies - it should contain a session cookie. You will also need to extract the various values for security_token, login_ticket and the other fields so that you can include them in the login POST request.

The key point is to use the same session that you used to get the tokens for all subsequent requests. Probably the server associates the session id with the tokens that it assigned, so you need to keep these together at the client end. By creating a new session after you get the tokens, as you are doing, the association is lost.

In code:

import requests

login = r'LOGIN_URL'
url = 'PROTECTED_URL'

session = requests.Session()

r = session.get(login)    # retrieve cookie, tokens, and other stuff

# parse the response to extract the tokens etc. (perhaps use BeautifulSoup). Store in "values". Add username and password to "values"

r = session.post(login, data=values)    # perform the login

r = session.get(url)                    # get the protected page


来源:https://stackoverflow.com/questions/38122379/performing-login-with-python-requests-cookies-not-activated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!