说我有这样的表格:
<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'使用机械化?
答案 0 :(得分: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']
UPD。使代码更漂亮