如何使用Python在表单中选择选项?

时间:2011-06-15 15:00:07

标签: python mechanize

我想知道如何在格式为

的表单中选择选项
  <td align="left">
                  <select name="FORM1" id="FORM1" multiple="multiple" size="5">
                      <option value="Value1">Value1</option>
                      <option value="Value2">Value2</option>
                  </select>
  </td>

现在,我正在使用mechanize连接到网站并遍历到所需的页面。此页面有多种形式,如FORM1,FORM2,FORM3等,带有选项。我想选择(启用)Value1然后告诉mechanize实例点击提交按钮。哪种方法可以根据表单名称启用选项?

2 个答案:

答案 0 :(得分:22)

以下是一些基本用法示例:

>>> import mechanize
>>> br = mechanize.Browser()
>>> br.open('http://www.w3schools.com/html/html_forms.asp')

表单具有name属性;有时它虽然是空的:

>>> [f.name for f in br.forms()]
['searchform', None, None, None, None, 'input0']

表格有一系列控制;控件也有名称:

>>> forms = [f for f in br.forms()]
>>> forms[1].controls[0].name
'firstname'
>>> [c.name for c in forms[3].controls]
['sex']

您可以在控件中获取项目列表:

>>> forms[3].controls[0].get_items()
[<Item name='male' id=None type='radio' name='sex' value='male'>, <Item name='female' id=None type='radio' name='sex' value='female'>]

对于单选按钮,您只需进行一次选择:

>>> forms[3]['sex'] = ['male']

但选择必须在列表中:

>>> forms[3]['sex'] = 'male'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.6/site-packages/mechanize/_form.py", line 2782, in __setitem__
    control.value = value
  File "/Library/Python/2.6/site-packages/mechanize/_form.py", line 1977, in __setattr__
    self._set_value(value)
  File "/Library/Python/2.6/site-packages/mechanize/_form.py", line 1985, in _set_value
    raise TypeError("ListControl, must set a sequence")
TypeError: ListControl, must set a sequence

对于复选框,您可以进行多项选择:

>>> [(c.name, c.get_items()) for c in forms[4].controls]
[('vehicle', [<Item name='Bike' id=None type='checkbox' name='vehicle' value='Bike'>, <Item name='Car' id=None type='checkbox' name='vehicle' value='Car'>])]
>>> forms[4]['vehicle'] = ['Bike', 'Car']

您可以找到更多信息here(链接从Matt Hempel :)被盗。

答案 1 :(得分:3)

当您说网页有多个表单时,您的意思是页面上有多个<form>元素,还是多个表单字段(例如<select>)?

python sketch out how to select list items的Mechanize文档。以下是他们提供的样本:

# Controls that represent lists (checkbox, select and radio lists) are
# ListControl instances.  Their values are sequences of list item names.
# They come in two flavours: single- and multiple-selection:
form["favorite_cheese"] = ["brie"]  # single

在您的情况下,选择Value1的代码如下所示:

form["FORM1"] = ["Value1"]