我正试图使用python请求模块登录一个应用程序的URL。但是,由于我的登录按钮没有 "name "标签,我无法登录。
下面是HTML表格。
<form id="login-submit-form" onsubmit="return beforesubmit()" action="/ui/login" method="post">
<input name="action" type="hidden" value="login">
<input name="useCACCertificate" type="hidden" value="false">
<input name="server" type="hidden" value="">
<input name="gwt.codesvr" type="hidden" value="">
<input name="urlhash" type="hidden" value="">
<input name="fromURI" type="hidden" value="/">
<input name="locale" type="hidden" value="">
<input name="username" tabindex="0" class="input-field" id="login-form-username" type="text" placeholder="Username" value=""> <br>
<input name="password" tabindex="0" class="input-field" id="login-form-login-password" type="password" placeholder="Password"> <br>
<div class="input-field" id="servers-input-field" style="border: currentColor; border-image: none; padding-left: 0px;">
<select tabindex="0" id="login-form-servers" style="width: 300px; display: none;">
<option value="Default Client">Default Client</option>
</select><span tabindex="0" class="ui-selectmenu-button ui-widget ui-state-default ui-corner-all" id="login-form-servers-button" role="combobox" aria-expanded="false" aria-haspopup="true" aria-owns="login-form-servers-menu" style="width: 298px;" aria-autocomplete="list"><span class="ui-icon ui-icon-triangle-1-s"></span><span class="ui-selectmenu-text">Default Client</span></span>
</div>
<div class="rememberMeContainer">
<input name="rememberMe" tabindex="0" class="rememberMeCheck" id="rememberme-checkbox"
type="checkbox">
<label for="rememberme-checkbox">Remember me</label>
</div>
<input tabindex="0" class="input-field login-button" id="login-form-login" type="submit"
value="Sign In">
</form>
What I have tried is a simple post on the URL:
from bs4 import BeautifulSoup
import requests
# Start the session
session = requests.Session()
# Create the payload
payload = {'username':'user1',
'password':'pwd1'
}
# Post the payload to the site to log in
s = session.post("https://xyz/ui", data=payload, verify = False)
s.text
我最终得到的登录页面html代码是s.text,这意味着我无法登录。
请帮我了解一下,到底漏掉了什么。
你的Python代码只发送了一个POST请求的两个参数。username
和 password
. 然而,它应该包括所有需要完成请求的参数。发现这些参数的一个长而明显的方法是检查表单中的输入标签。参数名称是输入标签的值。name
属性,其相关的值完成了键值对。
例如,你还必须有。useCACCertificate=false&server=&gwt.codesvr=&urlhash=&fromURI"=/&locale=
这不是一个肯定的,你可以省略空参数(没有值), 虽然它是可能的,一个请求省略这样的值会成功。它 完全取决于你正在与之互动的网站,完全不受你的控制。. 你还应该包括 <select>
框作为键值对,以及作为 记住我 复选框。
最快捷的方法是拦截原始请求。就个人而言,我使用 打嗝套房但你甚至可以使用浏览器中的开发工具,在 联网 标签来登录,从而拦截POST请求。你会在那里找到所有必要的键值对来完成请求。你也应该尝试模仿头,以确保你不会被WAF或其他什么东西阻止;因为默认的PyRequests头往往会经常被阻止。