如何使用python访问/设置HTML中的“select”标记

如何使用python访问/设置HTML中的“select”标记,python,html,selenium,web-scraping,mechanize,Python,Html,Selenium,Web Scraping,Mechanize,我试图从HTML页面中提取事件- 我想使用python选择不同的区域,但由于以下HTML而无法实现: <select data-ng-options="key as value.name for (key,value) in areaGroups | orderBy:'name'" data-ng-model="selectedAreaGroup" data-ng-change="updateAreaGroup()" class="ng-pristine ng-valid ng-touch

我试图从HTML页面中提取事件-

我想使用python选择不同的区域,但由于以下HTML而无法实现:

<select data-ng-options="key as value.name for (key,value) in areaGroups | orderBy:'name'" data-ng-model="selectedAreaGroup" data-ng-change="updateAreaGroup()" class="ng-pristine ng-valid ng-touched">
    <option value="" class="" selected="selected">Choose an area</option>
    <option value="string:CannockChase" label="Cannock Chase District">Cannock Chase District</option>
    <option value="string:EastStaffordshire" label="East Staffordshire">East Staffordshire</option>
    <option value="string:Lichfield" label="Lichfield District">Lichfield District</option>
    <option value="string:Newcastle" label="Newcastle Borough">Newcastle Borough</option>
    <option value="string:SouthStaffordshire" label="South Staffordshire">South Staffordshire</option>
    <option value="string:Stafford" label="Stafford Borough">Stafford Borough</option>
    <option value="string:StaffordshireMoorlands" label="Staffordshire Moorlands">Staffordshire Moorlands</option>
    <option value="string:SoTCentral" label="Stoke-on-Trent Central">Stoke-on-Trent Central</option>
    <option value="string:SoTNorth" label="Stoke-on-Trent North">Stoke-on-Trent North</option>
    <option value="string:SoTSouth" label="Stoke-on-Trent South">Stoke-on-Trent South</option>
    <option value="string:Tamworth" label="Tamworth Borough">Tamworth Borough</option>
我使用Mechanize在页面上查找表单,但由于标签上没有附加表单,我无法确定如何选择它,然后提交值


我的最佳选择是什么?

您可以根据表单在页面上的显示顺序选择表单,首先导入并打开

import mechanize
br = mechanize.Browser()
br.open('http://www.staffordshire-pcc.gov.uk/space/')
循环浏览页面中的所有表单

forms = [f.name for f in br.forms()]
让我们检查表单[0]是否是表单的正确索引,下拉列表与问题中的一样设置控制变量并打印出值

control = forms[0].controls[0]
form_values = [item.attrs['value'] for item in control.items]
print form_values
如果这是正确的形式,您应该看到:

["string:CannockChase", "string:EastSta....
如果未找到正确的索引,请循环浏览索引*见下文

最后,找到正确的表单后,您可以设置一个值并提交:

br.form[0*] = form_values[0]
r = br.submit()
// read out the HTML from the resulting page
print r.read()

*此索引是您问题中表示下拉表单的索引

我已经完成了一半,但无法计算出该项。attrs['value']位。谢谢你抽出时间。