使用Python登录到Ajax形式
-
25-09-2019 - |
题
顺便说一句...此网站仅与Internet Explorer一起使用。
我正在尝试为客户介绍网站,以便我可以为他们自动化任务。基本上,它将刮擦不同的报告,并寻找转折时间并将其通过电子邮件发送给客户。我的取消程序效果很好,我遇到的问题是用机械化登录网站,因为登录表格正在使用AJAX。我已经四处寻找解决方案,但似乎找不到我想要的。
以下是HTML形式,(从我可以看出的)处理它的Ajax。
函数tranlogin(){var url ='login.aspx?isajax = true&eventTarget = tranlogin'; var postdata = form.serialize('form1'); ajaxrequest = new ajax.request(url,{方法:'post',postbody:postdata,oncomplete:transoginfined,onfailure:reporterror:reporterror,onexception:report exception}); }
函数转换(serverResponse){if(requestFailed)return; xmlnodes = serverresponse.responsexml; usrsite =“ 8000”; usrcode = decodexmlchar(xmlnodes.getElementsBytagName('usercode')[0] .Text); if(xmlnodes.getElementsByTagName('loginResult')[0] .Text =='-1'){alert(decodexmlchar(xmlnodes.getElementsbybytbybytagname('failmsg'')[0] .text)); form.enable('form1');返回; } if(xmlnodes.getElementsBytaGname('loginResult')[0] .Text =='20'){window.location.replace('initaspasswd.aspx?usersite =' + usrsite +' + usrsite +'&usercode =' + usrcode);返回; } if(xmlnodes.getElementsBytaGname('loginResult')[0] .Text =='14'){window.location.replace('chgpasswd.aspx?type = chgpwd&usersite = chgpwd&usersite =' + usrsite + usrsite +'&usercode'&usercode =' + usrcode);返回; } if(xmlnodes.getElementsbybytagname('loginResult')[0] .Text =='16'){window.location.replace('chgpasswd.aspx?type = pwdexpire&usersite&usersite&usersite&usersite =' + usrsite +' + usrsite +'&usercode'&usercode =' + usrcode);返回; } if(xmlnodes.getElementsbybytagname('loginResult')[0] .Text =='0'){if(xmlnodes.getElementsbybytagname('pwdexpirewarning'')[0] .Text =='true true truepwdnow = var offerpwdnow = window = window。确认(decodexmlchar(xmlnodes.getElementsbytagname('pwdexpiremsg')[0] .Text)); if(thatspwdnow == true){window.location.replace('chgpasswd.aspx?type = chgpwd&usersite =' + usrsite +'&usercode =' + usrcode);返回; } // var arg = {striptmsg:decodexmlchar(xmlnodes.getElementsBytaGname('pwdexpiperemsg')[0] .Text),// buttnons:[yes yes:“ yes”,“”,rtnval:rtnval:rtnval:1},// “,rtnval:0} //] //}; // var rtn = window.showmodaldialog('../ modules/modalmessagebox.aspx',arg,“ dialogheight:140px; dialogogwidth:500px; center:1; center:1; status; status; status; noad; no;“;”); // if(rtn && rtn == 1){// window.location.replace('chgpasswd.aspx?type = chgpwd&usersite =' + usrsite +'&usercode =' + usercode =' + usrcode); // 返回; //}
} if(jtrim($($('txthospcode')。值)==''){hosplist = decodexmlchar(xmlnodes.getElementsbytybytagname('hosplist')[0] .text).split('split).split('|'|'); if(hosplist.length <2){selectedhospcode = hosplist [0] .split(' - ')[0]; Transelecthosp(SelectedhospCode);返回; } $('divhosplist')。style.display ='block';
for(i=0;i<hospList.length;i++)
{
if (hospList[i] != '')
{
divHospCode = document.createElement("div");
divHospCode.className='divHospCode';
$('divHospListBG').appendChild(divHospCode);
lnkHospCode = document.createElement("a");
if (hospList[i].length <= 33)
lnkHospCode.innerText = hospList[i];
else
lnkHospCode.innerText = hospList[i].substr(0,30) + '...';
lnkHospCode.title = hospList[i];
lnkHospCode.className = 'lnkHospCode';
divHospCode.appendChild(lnkHospCode);
lnkHospCode.onmouseover = function(){this.style.color = '#000000';}
lnkHospCode.onmouseout = function(){this.style.color = '#6c6c6c';}
lnkHospCode.onclick = function(){TranSelectHosp(this.innerText.split('-')[0]);}
if (i > 7 && $('divHospListBG').style.overflow != 'auto')
{
$('divHospListBG').style.height = '198px';
$('divHospListBG').style.overflow = 'auto';
}
}
}
return;
}
else
{
TranSelectHosp(JTrim($('txtHospCode').value));
}
}
}
<form name="Form1" method="post" action="login.aspx" id="Form1">
输入帐户代码: <div class="divRight">
<input name="txtHospCode" type="text" id="txtHospCode" class="inputClass" maxlength="4" />
</div>
<div class="divLeft">
<span>Input User Code:</span>
</div>
<div class="divRight">
<input name="txtUserCode" type="text" id="txtUserCode" class="inputClass" maxlength="6" />
</div>
<div class="divLeft">
<span>Input Password:</span></div>
<div class="divRight">
<input name="txtPassword" type="password" id="txtPassword" class="inputClass" />
</div>
<div class="divLeft">
<span>Login As:</span>
</div>
<div class="divRight">
<input type="radio" name="rdLoginType" value="D" checked="checked" />Doctor
<input type="radio" name="rdLoginType" value="T" />Other
</div>
<div class="divLeft">
</div>
<div class="divRight">
<input class="buttonClass" id="btnOK" type="button" value="Enter" onclick="LoginIn();" />
<input class="buttonClass" id="btnReset" type="button" value="Reset" onclick="ResetInput();" />
</div>
到目前为止我的代码
import mechanize
import cookielib
from BeautifulSoup import BeautifulSoup
import html2text
import re
Instatiate浏览器
br = Mechanize.Browser()
饼干罐
cj = cookielib.lwpcookiejar()br.set_cookiejar(CJ)
浏览器选项
br.set_handle_equiv(true)
br.set_handle_gzip(true)
br.set_handle_redirect(true)br.set_handle_referer(true)br.set_handle_robots(false)
遵循刷新0但不挂在刷新上> 0
br.set_handle_refresh(Mechanize._http.httprefreshprocessor(),max_time = 1)
用户代理
br.Addheaders = [('用户代理','Mozilla/4.0(兼容; MSIE 7.0B; Windows NT 6.0)')]]
def login_to_website(login_url,login_form_name,usr_form_name,pwd_form_name,acct_code_name,usr,pwd,acct_code):“
# Open the url of the login page
br.open(login_url)
# Select the login form name
br.select_form(login_form_name)
# Enter user's credentials into the form
br.form[acct_code_name] = acct_code
br.form[usr_form_name] = usr
br.form[pwd_form_name] = pwd
br.find_control(name='rdLoginType').value = ['T']
# Submit the form
print "Logging in as:", usr
br.submit()
# print current url
print "We are now at:", br.geturl()
# print error
if br.geturl() == login_url:
print "Login Failed"
else: print "Successfully logged in"
login_to_website('https://www.website.com','form1','txtusercode','txtpassword','txthospcode',txthospcode',usr,pwd,acctcode)
解决方案
据我所知,机械化不会处理JavaScript。因此,您的选择是,以我尝试的方式进行粗略的顺序:
- 在浏览器中关闭JavaScript,看看是否仍然可以登录该站点。如果是这样,请尝试在该过程中使用机械化。
- 尝试弄清Ajax表单的效果是什么(服务器端和客户端),并尝试使用Python模拟它。如果您还没有找到它,那么诸如Firebug之类的东西对于此是无价的。
- 使用允许Python控制真实浏览器的各种库之一。我从来没有这样做,但是我知道至少有Firefox和IE的包装器。