如何使用 python 在 HTML 中 access/set 'select' 标记

How to access/set 'select' tag in HTML with python

我正在尝试从 HTML - http://www.staffordshire-pcc.gov.uk/space/

的页面中提取事件

我想 select 使用 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 在页面上查找表单,但由于标签上没有附加表单,所以我不知道如何 select 它,然后提交一个值。

我追求的最佳选择是什么?

您可以select按页面上出现的顺序填写表格,先导入&打开

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()

* 该索引代表您问题中的下拉表单