name and value lookup collection, loaded from a dropdown list using beautifulsoup
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 wit开发者_如何学Pythonh this collection/dictionary, I want to be able to lookup by both 'Some Text' and 234234234239393.
Is this possible?
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")
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
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
精彩评论