KeyError:登录到ASP.NET使用python请求的网站?

2024-07-04 05:47:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用python登录.aspx网站请求。这个与其他网站正常工作,但弄乱了.aspx网站。总之

我尝试过的:
(简单但由于名字的原因看起来很复杂,请检查代码)

headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36",
"X-MicrosoftAjax": "Delta=true",
"X-Requested-With": "XMLHttpRequest"}

with requests.Session() as s:
url = 'https://nucleus.niituniversity.in/Default.aspx'
soup = BeautifulSoup(s.get(url).content)

login_data = {'ScriptManager1': 'SchSel$up|SchSel$btnLogin',
              '__LASTFOCUS': '',
              '__EVENTTARGET': '',
              '__EVENTARGUMENT': '',
              '__VIEWSTATE': soup.select_one("#__VIEWSTATE")["value"],
              '__VIEWSTATEGENERATOR': soup.select_one("#__VIEWSTATEGENERATOR")["value"],
              'SchSel$cmbUserType': 'Student',
              'SchSel$txtUserName': 'user',
              'SchSel$txtPassword': '1234',
              'SchSel$chkSkip': 'on',
              'SchSel$hidLastLogin': "Last Login: NIIT University | 2016-2017",
              'SchSel$hidSchoolId': soup.select_one("#SchSel_hidSchoolId")["value"],
              'SchSel$hidSchoolName': soup.select_one("#SchSel_hidSchoolName")["value"],
              'SchSel$hidCoreUserDet': soup.select_one("#SchSel_hidCoreUserDet")["value"],
              'SchSel$hidSessionId': '8',
              'SchSel$hidActiveSchool': '1',
              'SchSel$hidHitCount': soup.select_one("#lblTotalHit").text.split(":", 1)[1].strip(),
              'SchSel$hidReportingDet': '0',
              'SchSel$hidActiveSession': '1',
              'SchSel$hidSchoolAddress': 'Address',
              'SchSel$hdnLoginAsUser': '',
              '__ASYNCPOST': 'true',
              'SchSel$btnLogin.x': '42',
              'SchSel$btnLogin.y': '14'
              }
s.post(url, data=login_data, headers=headers)

res = s.get('Protected page')

这会引发一个错误

KeyError: 'value' error for (soup.select_one("#SchSel_hidCoreUserDet")["value"])


所以,当我看完要登录的网页的源代码时。这是我要登录的网页的页面源代码部分。

if (obj != null) {
        obj.onreadystatechange = ProcessResult;
        obj.open("GET", "GetLoginData.aspx?username=" + userName + "&categoryid=" + categoryId + "&URL=" + document.URL + "&TIME=" + new Date(), true);
        obj.send(null);
    }
    return false;
}<br>

处理结果函数:

 function ProcessResult() {
    var stock;
    if (obj.readyState == 4) {
        if (obj.status == 200) {
            var cmbUserType = document.getElementById("SchSel_cmbUserType");
            var categoryId = cmbUserType.selectedIndex;
            var chkAutoLogin = document.getElementById("SchSel_chkAutoLogin");

            if (obj.responseText != "") {
                var dataarr = obj.responseText.split(':');
                var lblSchoolHit = document.getElementById("lblSchoolHit");
                var lblCount = document.getElementById("lblCount");
                var lblLastLogin = document.getElementById("SchSel_lblLastLogin");
                var hidCoreUserDet = document.getElementById("SchSel_hidCoreUserDet");
                var hidSchoolId = document.getElementById("SchSel_hidSchoolId");
                var hidSessionId = document.getElementById("SchSel_hidSessionId");
                var hidActiveSchool = document.getElementById("SchSel_hidActiveSchool");
                var hidReportingDet = document.getElementById("SchSel_hidReportingDet");
                var hidActiveSession = document.getElementById("SchSel_hidActiveSession");
                var hidHitCount = document.getElementById("SchSel_hidHitCount");
                var hidSchoolAddress = document.getElementById("SchSel_hidSchoolAddress");

                if (dataarr[0] == "" || dataarr[0] == null) {
                    chkAutoLogin.checked = true;
                    chkAutoLogin.disabled = false;
                    lblLastLogin.innerHTML = " Last Login: " + hidSchoolName.value + " | " + arrlist[4];
                    hidCoreUserDet.value = arrlist[0];
                    hidSchoolId.value = arrlist[2];
                    hidSessionId.value = arrlist[3];
                    document.getElementById("SchSel_hidLastLogin").value = "";
                    lblLastLogin.innerHTML = " Last Login: " + hidSchoolName.value + " | " + arrlist[4];
                    hidActiveSchool.value = arrlist[5];
                    hidReportingDet.value = arrlist[6];
                    hidActiveSession.value = arrlist[7];
                    lblSchoolHit.style.visibility = "hidden";
                    lblCount.style.visibility = "hidden";
                }
                else {
                    var arrlist = dataarr[0].split('|');
                    hidCoreUserDet.value = arrlist[0];
                    if (hidCoreUserDet.value != "") {
                        var hidSchoolName = document.getElementById("SchSel_hidSchoolName");
                        hidSchoolName.value = arrlist[1];
                        hidSchoolId.value = arrlist[2];
                        hidSessionId.value = arrlist[3];
                        hidActiveSchool.value = arrlist[5];
                        hidReportingDet.value = arrlist[6];
                        hidActiveSession.value = arrlist[7];
                        hidHitCount.value = arrlist[8];
                        hidSchoolAddress.value = arrlist[9];
                        lblLastLogin.innerHTML = "Last Login: " + hidSchoolName.value + " | " + arrlist[4];
                        document.getElementById("SchSel_hidLastLogin").value = lblLastLogin.innerHTML;

我确信给SchSel$hidCoreUserDet的值是有问题的,因为只有当obj.response not =""值被赋值时。
你知道怎么解决这个问题吗?你知道吗


Tags: objifvaluevarselectdocumentonesoup

热门问题