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

Flu*_*ffy 5 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'
Run Code Online (Sandbox Code Playgroud)

ABe*_*oon 17

您需要先将控件添加到表单,然后再添加fixup表单.

br.form.new_control('text','unexistent',{'value':''})
br.form.fixup()
br['unexistent'] = 'hello'
Run Code Online (Sandbox Code Playgroud)

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

This method should only be called once, after all controls have been
added to the form.
Run Code Online (Sandbox Code Playgroud)

但是,看起来它看起来不太危险.可能至少在弄乱表单中的任何其他内容之前先添加控件.