Differentiation between the html form of SELECT elements with the same name

I am trying to dynamically populate a form in Python using Mechanize. However, when I checked the source of the HTML page with the form, I realized that some controls on the form have the same name. Here is an excerpt from the form:

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

Is there a way to get the possible elements of each SELECT control without identifying them by name / id?

+1
source share
2 answers

You can use BeautifulSoup to parse the answer to get selection options

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

You cannot do something like this br['mv_searchspec'] = 'foo', because obviously this is ambiguous. You have to do it though

br.select_form(nr=0) # select index
controls = br.form.controls
controls[desired_index]._value = 'your_value'
...
br.submit()
+5

zneak, . , "" POST/GET, . , .

0

All Articles