1

假设我有这样的表格:

<td>
    <select name="text1" class="long" >
     <option value="">-- unknown --</option>
     <option value="1">test1</option>
     <option value="2">test2</option>
     <option value="3">test3</option>
     <option value="4">test4</option>
    </select>
</td>

有没有办法获得所有可能的选项值,在这种情况下:'','1','2','3','4'使用机械化?

4

1 回答 1

3

首先,您需要实际的 html 表单:

html = '''
<form>
<td>
    <select name="text1" class="long" >
     <option value="">-- unknown --</option>
     <option value="1">test1</option>
     <option value="2">test2</option>
     <option value="3">test3</option>
     <option value="4">test4</option>
    </select>
</td>
</form>
'''
forms = mechanize.ParseString(html, 'fake')
form = forms[1] # 0th is kind of default one here

control = form.controls[0] # the select
print [item.attrs['value'] for item in control.items]

Out: ['', '1', '2', '3', '4']

更新。让代码更漂亮

于 2012-09-06T09:57:20.800 回答