我在 Python 中使用 mechanize 。我无法设置 SelectControl 元素的值。
我要抓取的网页是http://www.mcxindia.com/SitePages/indexhistory.aspx
我关心的部分源代码是:
<tr>
<td bgcolor="#f5f4f5" class="txtblue"><table border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><table id="mRbtLstSpotFut" border="0" style="font-weight:normal;height:85px;width:208px;">
<tr>
<td><input id="mRbtLstSpotFut_0" type="radio" name="mRbtLstSpotFut" value="1" checked="checked" /><label for="mRbtLstSpotFut_0">Commodity Future Indexes</label></td>
</tr><tr>
<td><input id="mRbtLstSpotFut_1" type="radio" name="mRbtLstSpotFut" value="0" onclick="javascript:setTimeout('__doPostBack(\'mRbtLstSpotFut$1\',\'\')', 0)" /><label for="mRbtLstSpotFut_1">Commodity Spot Indexes</label></td>
</tr><tr>
<td><input id="mRbtLstSpotFut_2" type="radio" name="mRbtLstSpotFut" value="2" onclick="javascript:setTimeout('__doPostBack(\'mRbtLstSpotFut$2\',\'\')', 0)" /><label for="mRbtLstSpotFut_2">Rainfall Indexes</label></td>
</tr>
</table></td>
<td> </td>
<td rowspan="3" valign="top"><p style="margin-top: 3px; margin-bottom: 0; margin-left: 0" align="justify">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td class="tablerowtxt1"> Selected Index</td>
<td> </td>
<td colspan="2"><select name="mDdlOtherIndex" id="mDdlOtherIndex" class="dd" style="width:170px;">
<option selected="selected" value="323">MCXCOMDEX</option>
<option value="324">MCXMETAL</option>
<option value="325">MCXENERGY</option>
<option value="326">MCXAGRI</option>
</select></td>
现在我可以更改我正在做的第二个单选按钮的值
br.select_form(nr=0)
br.set_all_readonly(False)
br.form['mTbFromDate']='08/01/2013'
br.form['mTbToDate']='08/08/2013'
br.form.set_value(['0'],name='mRbtLstSpotFut')
但我也想选择不存在的选项值'327'(因为它在单击单选按钮时由 JS 出现,机械化无法处理)
所以我正在尝试向表单添加一个新控件,例如
<option value="327">MCXENERGY</option>
我正在尝试这个,但这不起作用。
br.form.new_control('text','option',{'value':'327'})
br.form.fixup()
br.form['mDdlOtherIndex']= ['327']
但这给了我以下错误:
File "build\bdist.win-amd64\egg\mechanize\_form.py", line 2006
mechanize._form.ItemNotFoundError: insufficient items with name '327'
我哪里错了?