2011-09-18 16 views
5

,我试图使机械化解析HTML是:Python机械化:如何选择一个下拉列表,当两个在网页中具有相同的名称?

<select id="topic_id2" name="topics[]" title="blabla" tabindex="4" class="createSelect"> 
here go options 

但随后右下方它还有另外一个下拉列表中,用下面的代码:

<select id="topic_id3" name="topics[]" title="optional" tabindex="5" class="createSelect"> 

现在,如果它有助于在所有,我不需要从后者中选择任何值,因为它是可选的。

当我尝试

br = mechanize.Browser() 
br.select_form(name="form") 
br["topics[]"] = ["Internet"] 

我得到:

mechanize._form.AmbiguityError: more than one control matching name 'topics[]' 

有没有一种方法,我可以根据其ID选择控制,使用mechanize.Browser()(同时保留所有其他形式语法)?

感谢

+5

你可以通过它们的索引在窗体中访问控件,看到这个答案http://stackoverflow.com/questions/6482308/differentiating-between-html-form-select-items-with-the-same-name/ 6483458#6483458 – cerberos

+0

非常感谢,这工作。 –

+0

你能在链接中给出答案吗? – cerberos

回答

1

mechanize外部文档是相当小的,只包含几个例子,但在代码文件是要广泛得多。

没有测试过这个,HTMLForm实例叫做form你应该可以调用form.find_control(id="topic_id3")并得到你想要的。我不知道如何仅使用Browser对象执行此操作,但是您是否尝试过br.find_control(id="topic_id3")

相关问题