How to access/set 'select' tag in HTML with python
Question: I'm trying to extract events from a page of HTML - <http://www.staffordshire-
pcc.gov.uk/space/>

I want to select different areas using python but came unstuck with the
following 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>
    

I use Mechanize to find forms on pages, but as there's no form attached to the
tag, I can't work out how I would select it, and then submit a value.

What's the best option for me to pursue?


Answer: You can select the form by the order at which it appears on the page, firstly
import & open

    
    
    import mechanize
    br = mechanize.Browser()
    br.open('http://www.staffordshire-pcc.gov.uk/space/')
    

Loop through all the forms in the page

    
    
    forms = [f.name for f in br.forms()]
    

Lets check whether form[0] is the correct index for the form with the dropdown
(as in your question) Set the control variable and print out the values

    
    
    control = forms[0].controls[0]
    form_values = [item.attrs['value'] for item in control.items]
    print form_values
    

If this it the correct form you should see:

    
    
    ["string:CannockChase", "string:EastSta....
    

If not cycle through the indexes until you find the correct index (* see
below).

Finally once you have found the correct form you can set a value and submit:

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

* this index is whichever represents the dropdown form as in your question

