Reputation: 11
I have following html code:
<div class="6u">
<select name="PropertyId" tabindex="13">
<option value='0'>Select Property</option>
<option value='190' >Alderton Crescent</option>
<option value='9' >Cambridge Way</option>
<option value='11' selected>Corney Avenue</option>
<option value='8' >Stanley Gardens</option>
</select>
</div>
I am trying to extract only the selected option- in this case Corney Avenue. the code which i'm using:
soup.find('select', {'name': 'PropertyId'}).find_all({'option': 'selected'})
returns almost the same but into kind of a list:
[<option value="0">Select Property</option>
<option value='0'>Select Property</option>
<option value='190' >Alderton Crescent</option>
<option value='9' >Cambridge Way</option>
<option value='11' selected>Corney Avenue</option>
<option value='8' >Stanley Gardens</option>]
Upvotes: 0
Views: 765
Reputation: 25048
Alternativ approach could be to use the css selector
in combination with .text
/ get_text()
- Following line will grab <option>
that is selected
and part of <select>
named "PropertyId":
soup.select_one('select[name="PropertyId"] option:checked').text
from bs4 import BeautifulSoup
html='''<div class="6u">
<select name="PropertyId" tabindex="13">
<option value='0'>Select Property</option>
<option value='190' >Alderton Crescent</option>
<option value='9' >Cambridge Way</option>
<option value='11' selected>Corney Avenue</option>
<option value='8' >Stanley Gardens</option>
</select>
</div>'''
soup=BeautifulSoup(html,'lxml')
soup.select_one('select[name="PropertyId"] option:checked').text
Corney Avenue
Upvotes: 0
Reputation: 550
This should work-
from bs4 import BeautifulSoup
html="""<div class="6u">
<select name="PropertyId" tabindex="13">
<option value='0'>Select Property</option>
<option value='190' >Alderton Crescent</option>
<option value='9' >Cambridge Way</option>
<option value='11' selected>Corney Avenue</option>
<option value='8' >Stanley Gardens</option>
</select>
</div>"""
soup=BeautifulSoup(html,'lxml')
#find the select tag
select=soup.find('select',attrs={'name':'PropertyId'})
#find option value in select tag where 'selected' attribute is present
value=select.find('option',attrs={'selected':True})
print(value.text)
Upvotes: 1