How to get the text of selected option from HTML structure using python?
<select class="required" id="PPT" name="PPT">
<option value="">Paying Term</option>
<option selected="selected" value="0-10">0-10 Years</option>
<option value="10-20">10-20 Years</option>
<option value="20-30">20-30 Years</option>
<option value="30-40">30-40 Years</option>
</select>
This should help u:
from bs4 import BeautifulSoup as soup
html_soup = soup('<select class="required" id="PPT" name="PPT"><option value="">Paying Term</option><option selected="selected" value="0-10">0-10 Years</option><option value="10-20">10-20 Years</option><option value="20-30">20-30 Years</option><option value="30-40">30-40 Years</option></select>','html.parser') #Creates a BeautifulSoup object
opt = html_soup.find_all('option',selected = "selected") #Finds all occurrences under the option tag where selected = "selected"
for x in opt:
print(x.text)
Output:
0-10 Years
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.