使用Python请求执行登录(未激活Cookie) [英] Performing Login with Python requests (Cookies not activated)

查看:234
本文介绍了使用Python请求执行登录(未激活Cookie)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python请求模块执行登录.我首先用用户名和密码发布到登录页面,以保存响应中的cookie.之后,我尝试进入受密码保护的页面.问题是响应页面上说我必须激活cookie才能登录到此页面,但是我得到了可以显示的cookie. 这是代码

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 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">

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

Python代码:

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)

(我之前从另一个获取请求中获取了安全令牌和登录票) 因此,r.content打印出一个网页,该网页基本上显示:登录未成功,因为未激活Cookies.

(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.

但是r.cookies的输出是:

But the output from the r.cookies is:

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

如果未登录,则s.get会返回您获得的登录页面,并尝试访问受保护的页面.

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

所以我认为一切正常,只是网页认为我不保存发送的cookie.我该如何解决?

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?

编辑(解决方案):

工作,谢谢!

BeautifulSoup解决方案:

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)

推荐答案

使用r = session.get(login)尝试登录页面的初始GET.然后检查session.cookies-它应包含一个会话cookie.您还需要提取security_tokenlogin_ticket和其他字段的各种值,以便可以将它们包括在登录POST请求中.

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.

关键是要使用用于获取所有后续请求令牌的 same 会话.服务器可能将会话ID与它分配的令牌相关联,因此您需要在客户端将它们保持在一起.通过在获得令牌后创建一个新会话(如您所做的那样),关联将丢失.

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.

在代码中:

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

这篇关于使用Python请求执行登录(未激活Cookie)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆