K M*_*hta 1 html python mechanize
我正在尝试使用Mechanize在Python中动态填充表单.但是,当我检查具有该表单的HTML页面的源时,我意识到表单上的某些控件具有相同的名称.以下是表格的摘录:
<form action="[some website]" method=post>
<table>
    <tr><td>
        <select NAME="mv_searchspec" size="1">      
            <option VALUE="1119">Fall 2011 (1119)</option> 
            <!-- other options here -->
        </select>
    </tr><td>
    <tr><td>
        <select NAME="mv_searchspec" size="1"> 
            <option VALUE="">Select Department</option> 
            <option VALUE="ACC">ACC</option> 
            <!-- other options here -->
        </select>
    </tr></td>
</table>
</form>
有没有办法获取每个SELECT控件的possible_items而不用名称/ id识别它们?
您可以使用BeautifulSoup来解析响应以获取选择选项
import mechanize
from BeautifulSoup import BeautifulSoup
br = mechanize.Browser()
resp = br.open('your_url')
soup = BeautifulSoup(resp.get_data())
second_select = soup.findAll('select', name="mv_searchspec")[1]
# now use BeautifulSoup API to get the data you want
你不能做这样的事情br['mv_searchspec'] = 'foo',显然这是一种暧昧.你应该能够做到这一点
br.select_form(nr=0) # select index
controls = br.form.controls
controls[desired_index]._value = 'your_value'
...
br.submit()
| 归档时间: | 
 | 
| 查看次数: | 1146 次 | 
| 最近记录: |