【问题标题】:How to set value of hidden form in Mechanize/Python?如何在 Mechanize/Python 中设置隐藏形式的值?
【发布时间】:2011-08-01 07:34:17
【问题描述】:

我正在抓取一个网站,该网站使用隐藏表单作为针对我正在尝试做的事情的对策。这种形式:

<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();
}

聪明。我正在使用机械化抓取页面,如何设置此表单项的值?当我在 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])>
>

因为它显示为“只读”,所以我不能修改它,否则它会引发异常。肯定有解决方法,对吧?有什么想法吗?

【问题讨论】:

    标签: python screen-scraping mechanize


    【解决方案1】:

    正如elsewhere 发布的那样(即在机械化库的常见问题页面上):

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-07
      • 1970-01-01
      • 2011-09-25
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      • 2015-05-09
      • 1970-01-01
      相关资源
      最近更新 更多