2002/12

Find the selected option with BeautifulSoup

I would like to get only selected select options. For example:

<select> <option value="0">2002/12</option> <option value="1">2003/12</option> <option value="2">2004/12</option> <option value="3">2005/12</option> <option value="4">2006/12</option> <option value="5" selected>2007/12</option> </select> 

I know what I can do

 theSelectTag.findAll('option',attrs={'selected':''}) 

but this returns all parameters. Is there a way to get all the elements where the attribute exists? Please note that I ask everyone, since the site I am cleaning includes the selected attribute for several parameters.

I am using Python 2.7 and Beautiful Soup 4.1.2

+7
source share
1 answer

Passing True , since the attribute value will match all elements with this attribute:

 >>> from bs4 import BeautifulSoup >>> soup = BeautifulSoup('''<select> ... <option value="0">2002/12</option> ... <option value="1">2003/12</option> ... <option value="2">2004/12</option> ... <option value="3">2005/12</option> ... <option value="4">2006/12</option> ... <option value="5" selected>2007/12</option> ... </select>''') >>> soup.find_all('option', selected=True) [<option selected="" value="5">2007/12</option>] >>> soup.find_all('option', {'selected': True}) [<option selected="" value="5">2007/12</option>] 

And with lxml:

 >>> from lxml import etree >>> root = etree.HTML('''<select> <option value="0">2002/12</option> <option value="1">2003/12</option> <option value="2">2004/12</option> <option value="3">2005/12</option> <option value="4">2006/12</option> <option value="5" selected>2007/12</option> </select>''') >>> root.xpath('//option[@selected]') [<Element option at 0x228b7d0>] 
+7
source

All Articles