views:

27

answers:

3

On my html page I have a dropdown list:

<select name="somelist">
   <option value="234234234239393">Some Text</option>
</select>

So do get this list I am doing:

ddl = soup.findAll('select', name="somelist")

if(ddl):
    ???

Now I need help with this collection/dictionary, I want to be able to lookup by both 'Some Text' and 234234234239393.

Is this possible?

+2  A: 

Try the following to get started:

str = r'''
<select name="somelist">
   <option value="234234234239393">Some Text</option>
   <option value="42">Other text</option>
</select>
'''

soup = BeautifulSoup(str)
select_node = soup.findAll('select', attrs={'name': 'somelist'})

if select_node:
    for option in select_node[0].findAll('option'):
        print option

It prints out the option nodes:

<option value="234234234239393">Some Text</option>
<option value="42">Other text</option>

Now, for each option, option['value'] is the value attribute, and option.text is the text inside the tag ("Some Text")

Eli Bendersky
+1  A: 

Here's one way ..

ddl_list = soup.findAll('select', attrs={'name': 'somelist'})
if ddl_list:
    ddl = ddl_list[0]

    # find the optino by value=234234234239393
    opt = ddl.findChild('option', attrs={'value': '234234234239393'})
    if opt:
        # do something

    # this list will hold all "option" elements matching 'Some Text'
    opt_list = [opt for opt in ddl.findChildren('option') if opt.string == u'Some Text']
    if opt_list:
        opt2 = opt_list[0]
        # do something
ars
A: 

And again, just to show how one could do this with pyparsing:

html = r''' 
<select name="somelist"> 
   <option value="234234234239393">Some Text</option> 
   <option value="42">Other text</option> 
</select> 
''' 

from pyparsing import makeHTMLTags, Group, SkipTo, withAttribute, OneOrMore

select,selectEnd = makeHTMLTags("SELECT")
option,optionEnd = makeHTMLTags("OPTION")

optionEntry = Group(option("option") + 
                    SkipTo(optionEnd)("menutext") + 
                    optionEnd)

somelistSelect = (select.setParseAction(withAttribute(name="somelist")) +
                    OneOrMore(optionEntry)("options") +
                    selectEnd)

t,_,_ = somelistSelect.scanString(html).next()

for op in t.options:
    print op.menutext, '->', op.option.value

prints:

Some Text -> 234234234239393
Other text -> 42
Paul McGuire