Python添加一个新控件

2024-09-23 16:23:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我在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>&nbsp;</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>&nbsp;</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在单击单选按钮时出现的,mechanize无法处理)

因此,我尝试向窗体添加一个新控件,如

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

我哪里出错了?你知道吗


Tags: namebrformidvaluestyletablelabel
1条回答
网友
1楼 · 发布于 2024-09-23 16:23:18

我认为您需要创建select,然后选择。如果你没有选择,那么它就不能选择它。你知道吗

select的示例(选项位于select的内部):

<select>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

相关问题 更多 >