Pythonを使用してAjaxフォームにログインします
-
25-09-2019 - |
質問
ところで...このウェブサイトはInternet Explorerでのみ機能します。
クライアントのためにウェブサイトを廃棄しようとしているので、タスクを自動化できるようにしています。基本的には、さまざまなレポートをこすり、回転時刻を探してクライアントにメールで送信します。私のスクラッププログラムは正常に機能します。私が抱えている問題は、ログインフォームがAjaxを使用しているため、機械化してWebサイトにログインすることです。私は解決策を探していますが、私が探しているものを正確に見つけることができないようです。
以下は、HTMLフォームと(私が伝えることができることから)それを処理するAjaxです。
function tranlogin(){var url = 'login.aspx?isajax = true&eventtarget = tranlogin'; var postdata = form.serialize( 'form1'); ajaxrequest = new ajax.request(url、{method: 'post'、postbody:postdata、oncomplete:transloginfinished、onfailure:reporterror、onexception:reportexception}); }
function transloginfinished(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.getelementsbytagname( 'failmsg')[0] .text)); form.enable( 'form1');戻る; } if(xmlnodes.getElementsByTagname( 'loginResult')[0] .text == '20'){window.location.replace( 'initpasswd.aspx?usersite =' + usrsite + '&usercode =' + usrcode);戻る; } if(xmlnodes.getElementsByTagname( 'loginResult')[0] .text == '14'){window.location.replace( 'chgpasswd.aspx? type = chgpwd&usersite =' + usrsite + '&usercode =' + usrcode);戻る; } if(xmlnodes.getElementsByTagname( 'loginResult')[0] .text == '16'){window.location.replace( 'chgpasswd.aspx?type = pwdexpire&usersite =' + usrsite + '&usercode =' + usrcode);戻る; } if(xmlnodes.getElementsByTagname( 'loginResult')[0] .text == '0'){if(xmlnodes.getElementsBytagname( 'pwdexpirewarning')[0] .text == 'true'){var changepwdnow = window。確認(decodexmlChar(xmlnodes.getElementsByTagname( 'pwdexpiremsg')[0] .text)); if(changepwdnow == true){window.location.replace( 'chgpasswd.aspx?type = chgpwd&usersite =' + usrsite + '&usercode =' + usrcode);戻る; } // var arg = {promptmsg:decodexmlchar(xmlnodes.getelementsbytagname( 'pwdexpiremsg')[0] .text)、// buttons:[{value: "yes"、rtnval:1}、// {value: "no" "、rtnval:0} //] //}; // var rtn = window.showmodaldialog( '../ modules/modalmessagebox.aspx'、arg、 "dialogheight:140px; dialogwidth:500px; center:1; status:no;"); // if(rtn && rtn == 1){// window.location.replace( 'chgpasswd.aspx?type = chgpwd&usersite =' + usrsite + '&usercode =' + usrcode); // 戻る; //}
} if(jtrim($( 'txthospcode')。value)== ''){hosplist = decodexmlChar(xmlnodes.getElementsByTagname( 'hosplist')[0] .text).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
ブラウザを開始します
BR = MECANIZE.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(mecanize._http.httprefreshprocessor()、max_time = 1)
ユーザーエージェント
br.Addheaders = [( 'user-agent'、 '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): "" "" "logs ingle webサイト" "" ""
# 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'、usr、pwd、acctcode)
解決
機械化は、私が知る限り、JavaScriptを処理しません。あなたのオプションは、私が試してみることの大まかな順序でです:
- ブラウザでJavaScriptをオフにして、サイトにログインできるかどうかを確認します。その場合、そのプロセスで機械化を使用しようとします。
- Ajaxフォームの効果が何であるか(サーバー側とクライアント側の両方)を作成し、Pythonを使用してエミュレートしようとしてみてください。まだ見つけていない場合は、Firebugのようなものはこれに非常に貴重です。
- さまざまなライブラリのいずれかを使用して、Pythonが実際のブラウザを制御できるようにします。私はこれをしたことがありませんが、少なくともFirefoxとIEのためのラッパーがあることを知っています。