<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>
with this code, I need value as '0'
not text as '2002/12'
I tried a lot of BS4 options, .stripped_strings
, .strip()
, .contents
, get()
, etc.
How I can get values not text?
You want the value
attribute; access tag attributes using mapping syntax:
option['value']
Demo:
>>> 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>
... ''')
>>> for option in soup.find_all('option'):
... print 'value: {}, text: {}'.format(option['value'], option.text)
...
value: 0, text: 2002/12
value: 1, text: 2003/12
value: 2, text: 2004/12
value: 3, text: 2005/12
value: 4, text: 2006/12
value: 5, text: 2007/12
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With