我已经在线查看了其他示例,但是似乎无法弄清楚如何登录。页面看起来像这样:
<div class="Loginbox">
<form id="login_form" name="login_form" method="post" action="/a/login.seam" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="login_form" value="login_form" />
<div>
<p><label for="login_form:name">
Username</label><br /><input id="login_form:name" type="text" name="login_form:name"/>
</p>
</div>
<div><label for="login_form:password">
Password</label><br /><input id="login_form:password" type="password" name="login_form:password" value=""/>
</div>
<div><input id="login_form:submit" type="submit" name="login_form:submit" value="Login" class="buttonmed" />
</div><input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="-13463453453453:3453445345345345345" autocomplete="off" />
</form>
</div>
我尝试过这样的事情:
with requests.Session() as sess:
data = {}
data['login_form:name'] = user
data['login_form:password'] = password
sess.headers = HEADERS
sess.get(url)
sess.post(url, data=data)
h = sess.get(url)
print(h.text)
但是文字仍然显示我在登录页面上。
解决方案:
import requests
from bs4 import BeautifulSoup
with requests.Session() as sess:
data = {}
data['login_form'] = 'login_form'
data['login_form:name'] = user
data['login_form:password'] = password
sess.headers = HEADERS
g = sess.get(url)
soup = BeautifulSoup(g.text, "lxml")
val = soup.select_one("input[id='javax.faces.ViewState']")['value']
data['javax.faces.ViewState'] = val
h = sess.post(url, data=data)
print(h.text)
答案 0 :(得分:1)
您可以在浏览器控制台上查看所发送的请求,只要按页面上的登录按钮即可。您需要模拟此请求(URL和发布数据)。