Domanda

A proposito ... questo sito Web funziona solo con Internet Explorer ....

Sto tentando di eliminare un sito Web per un cliente in modo da poter automatizzare un'attività per loro. Fondamentalmente si raschia per diversi rapporti e cercherà i tempi di turno e viaggerà al cliente. Il mio programma di demolizione funziona bene, il problema che sto avendo sta accedendo al sito Web con meccanizzare poiché il modulo di accesso utilizza AJAX. Ho cercato una soluzione ma non riesco a trovare esattamente quello che sto cercando.

Di seguito è riportato il modulo HTML e (da quello che posso dire) l'Ajax che lo gestisce.

funzione tranlogin () {var url = 'login.aspx? Isajax = true & eventTarget = tranlogin'; var postData = form.serialize ('form1'); ajaxrequest = new ajax.request (url, {metodo: 'post', postbody: postData, oncomplete: translogInfiniture, onfailure: reporterror, Onexception: reportException}); }

funzione traslog xmlnodes = serverResponse.Responsexml; usRsite = "8000"; usrCode = decodexmlchar (xmlnodes.getElementsByTagName ('usercode') [0] .Text); if (xmlnodes.getElementsByTagName ('LogInsult') [0] .Text == '-1') {Alert (decodexMlchar (xmlnodes.getElementsByTagName ('failMSG') [0] .Text)); Form.enable ('form1'); Restituzione; } if (xmlnodes.getElementsByTagName ('LogInsult') [0] .Text == '20') {window.location.replace ('Initpasswd.aspx? Usilese =' + usRsite + '& usercode =' + usrCode); Restituzione; } if (xmlnodes.getElementsByTagName ('LogInsult') [0] .Text == '14') {window.location.replace ('chgpasswd.aspx? type = chgpwd & ussite =' + usrsite + '& usercode =' + usrcode); Restituzione; } if (xmlnodes.getElementsByTagName ('LogInsult') [0] .Text == '16') {window.location.replace ('chgpasswd.aspx? type = pwdexpire & ussite =' + usrsite + '& usercode =' + usrcode); Restituzione; } if (xmlnodes.getElementsByTagName ('LogInsult') [0] .Text == '0') {if (xmlNodes.getElementsByTagName ('pWDexPiPeWarning') [0] .Text == 'true') {var ChangePwdNow = Window. confermare (decodexmlchar (xmlnodes.getElementsByTagName ('pwdexpirEmsg') [0] .text)); if (ChangePwdNow == true) {window.location.replace ('chgpasswd.aspx? type = chgpwd & usersEite =' + usRsite + '& usercode =' + usrcode); Restituzione; } // var arg = {promptMsg: decodexmlchar (xmlnodes.getElementsByTagName ('pwdexpirEmsg') [0] .text), // pulsanti: [{valore: "Sì", rtnval: 1}, // {valore: "no ", rtnval: 0} //] //}; // var rtn = window.showmodaldialog ('../ moduli/modalmessagebox.aspx', arg, "dialogheight: 140px; dialogwidth: 500px; centro: 1; stato: no;"); // if (rtn && rtn == 1) {// window.location.replace ('chgpasswd.aspx? type = chgpwd & usersEite =' + usRSite + '& usercode =' + usrCode); // Restituzione; //}
} if (jtrim ($ ('txthospCode'). value) == '') {hosplist = decodexmlchar (xmlnodes.getElementsByTagName ('hosplist') [0] .Text) .Split ('|'); if (hosplist.length <2) {selectedHospCode = hosplist [0] .split ('-') [0]; Transelecthosp (selectedHospCode); Restituzione; } $ ('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">
Codice dell'account di input:
            <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>

Il mio codice finora


import mechanize
import cookielib
from BeautifulSoup import BeautifulSoup
import html2text
import re

Instatare il browser

br = meccanize.Browser ()

Barattolo di biscotti

CJ = cookielib.lwpcookiejar () br.set_cookiejar (CJ)

Opzioni del browser

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)

Segue Aggiorna 0 ma non è appeso all'aggiornamento> 0

br.set_handle_refresh (meccanize._http.httprefreshProcessor (), max_time = 1)

Agente utente

Br.AddEDers = [('User-Agent', 'Mozilla/4.0 (compatibile; 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, accecct_code): "" logs utente nel sito 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', 'txtusCode', 'txtpassword', 'txthospcode', usr, pwd, acctcode)

È stato utile?

Soluzione

Meccanize non elabora, per quanto ne so, JavaScript. Quindi le tue opzioni sono, in ordine approssimativo di ciò che proverei:

  • Spegni JavaScript nel browser e vedi se riesci ancora ad accedere al sito. In tal caso, tenta di utilizzare meccanizza con quel processo.
  • Prova a capire quale sia l'effetto del modulo AJAX (sia sul lato server che sul lato client) e cerca di emularlo usando Python. Se non l'hai già trovato, qualcosa come Firebug è inestimabile per questo.
  • Usa una delle varie librerie che consentono a Python di assumere il controllo di un vero browser. Non l'ho mai fatto, ma so che ci sono involucri per almeno Firefox e IE.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top