5

I'm using mechanize (which uses clientform) for some web crawling in python and since it doesn't support JS, I want to set a value of an unexistent input in a form (the input is generated by JS). How can I do this?

The error is similar to the one you get if you try to execute

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

1 回答 1

17

您需要先将控件添加到窗体,然后再添加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.

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

于 2009-10-10T20:45:03.963 回答