如何在Python ClientForm中设置不存在的字段?

如何在Python ClientForm中设置不存在的字段?,python,mechanize,clientform,Python,Mechanize,Clientform,我使用mechanize(它使用clientform)在python中进行一些web爬行,由于它不支持JS,我想在表单中设置一个不存在的输入值(该输入由JS生成)。我该怎么做 该错误与尝试执行时得到的错误类似 from mechanize import Browser br = Browser() page = br.open('http://google.com') br.select_form(nr = 0) br['unexistent'] = 'hello' 您需要首先将控件添加到表单

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

该错误与尝试执行时得到的错误类似

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

您需要首先将控件添加到表单,然后
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.
然而,它看起来并没有做任何太危险的事情。在处理表单中的任何其他内容之前,可能至少先添加控件

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