当两个在网页中具有相同名称时

当两个在网页中具有相同名称时

本文介绍了Python Mechanize:当两个在网页中具有相同名称时,如何选择下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要进行机械化解析的html是:

<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[]'

有没有一种方法可以使用mechanize.Browser()(同时保留所有其他形式的语法)基于其ID选择控件?

谢谢

解决方案

mechanize的外部文档非常小,仅包含一些示例,但是代码内的文档范围更广.

未经测试,使用名为formHTMLForm实例,您应该可以调用form.find_control(id="topic_id3")并获得所需的内容.我不确定如何仅使用Browser对象执行此操作,但是您是否尝试过br.find_control(id="topic_id3")?

The html that I'm trying to make mechanize parse is:

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

But then right below it there is another dropdown, with the following code:

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

Now if it helps at all, I need not select any value from the latter one, since it is optional.

When I try

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

I get:

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

Is there a way I can select a control based on its id, using mechanize.Browser() (while retaining all the other form syntax)?

Thanks

解决方案

The external documentation for mechanize is quite small and contains just a few examples, but the in-code documentation is far more extensive.

Not having tested this, with an HTMLForm instance called form you should be able to call form.find_control(id="topic_id3") and get what you want. I'm not sure how to do this with just a Browser object, but have you tried br.find_control(id="topic_id3")?

这篇关于Python Mechanize:当两个在网页中具有相同名称时,如何选择下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 03:42