【问题标题】:How to set an nonexistent field in Python ClientForm?如何在 Python ClientForm 中设置不存在的字段?
【发布时间】:2009-10-10 20:29:30
【问题描述】:

我正在使用 mechanize(它使用 clientform)在 python 中进行一些网络爬取,并且由于它不支持 JS,我想在表单中设置一个不存在的输入的值(输入由 JS 生成)。我该怎么做?

错误类似于您尝试执行时得到的错误

from mechanize import Browser
br = Browser()
page = br.open('http://google.com')
br.select_form(nr = 0)
br['unexistent'] = 'hello'

【问题讨论】:

    标签: python mechanize clientform


    【解决方案1】:

    你需要先将控件添加到表单中,然后fixup表单。

    br.form.new_control('text','unexistent',{'value':''})
    br.form.fixup()
    br['unexistent'] = 'hello'
    

    这确实没有很好的记录,在fixup() 下的源代码中有评论:

    This method should only be called once, after all controls have been
    added to the form.
    

    但是,它看起来并没有做太危险的事情。可能至少先添加控件,然后再弄乱表单中的任何其他内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-30
      相关资源
      最近更新 更多