[英]Scrape text from selected option from dropdown
如何使用python從HTML結構中獲取所選選項的文本?
<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>
這應該可以幫助你:
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)
輸出:
0-10 Years
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.