如何在Mechanize/Python中设置隐藏表单的值?

10 投票
1 回答
10572 浏览
提问于 2025-04-16 15:19

我正在抓取一个网站,这个网站使用了一个隐藏的表单来对抗我正在做的事情。这个表单:

<input style="width: 2px; height: 25px" type="hidden" size="1" name="TestJavaScript" /> 

就是问题所在。这个表单期望某个输入的值会被后面执行的JavaScript设置为“OK”:

function doSignOn() {
    window.document.tether.method = "POST";
    window.document.tether.action = "https://missionlink.missionfcu.org/MFCU/login.aspx";
    window.document.tether.TestJavaScript.value = "OK";

    if (window.document.tether.user.value.length < 1) {
        alert("Please enter your Member Number.");
        return;
    }

    if (window.document.tether.PIN.value.length < 1) {
        alert("Please enter your Password.");
        return;
    }

    // If we're in the service interruption or notice window, put up an alert.
    if (now <= interruption_end) {
        if (now >= notice_begin) {
            alert(prewarn_alert+'\n\nThank you.');
        }
    }
    window.document.tether.submit();
}

真聪明。我正在使用mechanize来抓取页面,我该如何设置这个表单项的值呢?当我在Python中打印form对象时,它看起来是这样的:

<tether POST https://missionlink.missionfcu.org/MFCU/login.aspx application/x-www-form-urlencoded
  <TextControl(user=)>
  <PasswordControl(PIN=)>
  <HiddenControl(TestJavaScript=) (readonly)>
  <SelectControl(signonDest=[*My Default Destination, Accounts.Activity, Accounts.Summary, Transfers.AddTransfer, SelfService.SelfService])>
>

由于它显示为“只读”,我无法修改它,否则会抛出异常。肯定有解决办法,对吧?有什么想法吗?

1 个回答

28

在其他地方(具体来说是在mechanize库的常见问题页面)提到过:

form.find_control("foo").readonly = False # allow changing .value of control foo 
form.set_all_readonly(False) # allow changing the .value of all controls

撰写回答