使用Python登录AJAX表单

时间:2011-01-05 23:59:03

标签: python ajax mechanize web-scraping

BTW ...此网站仅适用于Internet Explorer ....

我正在尝试为客户端废弃一个网站,以便我可以为他们自动执行任务。基本上它会抓取不同的报告,寻找转折时间并通过电子邮件发送给客户。我的报废程序运行正常,我遇到的问题是使用Mechanize登录网站,因为登录表单使用的是AJAX。我一直在寻找解决方案,但似乎无法找到我正在寻找的确切内容。

下面是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'); return; } if (xmlNodes.getElementsByTagName('LoginResult')[0].text == '20'){ window.location.replace('initpasswd.aspx?usersite=' + usrSite + '&usercode=' + usrCode); return; } if (xmlNodes.getElementsByTagName('LoginResult')[0].text == '14'){ window.location.replace('chgpasswd.aspx?type=chgpwd&usersite=' + usrSite + '&usercode=' + usrCode); return; } if (xmlNodes.getElementsByTagName('LoginResult')[0].text == '16'){ window.location.replace('chgpasswd.aspx?type=pwdexpire&usersite=' + usrSite + '&usercode=' + usrCode); return; } if (xmlNodes.getElementsByTagName('LoginResult')[0].text == '0'){ if (xmlNodes.getElementsByTagName('PwdExpireWarning')[0].text == 'true'){ var changePwdNow = window.confirm(decodeXmlChar(xmlNodes.getElementsByTagName('PwdExpireMsg')[0].text)); if (changePwdNow == true){ window.location.replace('chgpasswd.aspx?type=chgpwd&usersite=' + usrSite + '&usercode=' + usrCode); return; } // 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); // return; // }
} if (JTrim($('txtHospCode').value) == '') { hospList = decodeXmlChar(xmlNodes.getElementsByTagName('HospList')[0].text).split('|'); if (hospList.length < 2) { selectedHospCode = hospList[0].split('-')[0]; TranSelectHosp(selectedHospCode); return; } $('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));
            }
        }
     }

                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">
                &nbsp;<input type="radio" name="rdLoginType" value="D" checked="checked" />Doctor&nbsp;&nbsp;
                <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>

1 个答案:

答案 0 :(得分:1)

据我所知,Mechanize没有处理Javascript。所以你的选择按照我的尝试粗略排列:

  • 在浏览器中关闭Javascript,看看您是否仍然可以登录该网站。如果是这样,请尝试在该过程中使用机械化。
  • 尝试弄清楚AJAX表单的效果(服务器端和客户端),并尝试使用Python模拟它。如果你还没有找到它,像Firebug这样的东西是非常宝贵的。
  • 使用各种库之一,允许Python控制真正的浏览器。我从来没有这样做,但我知道至少有Firefox和IE的包装。