I'm trying to get the selected value from following HTML using BeautifulSoup but unable to.
<select id="bySeason" tconst="tt0944947" class="current">
<!--
This ensures that we don't wind up accidentally marking two options
(Unknown and the blank one) as selected.
-->
<option value="1">
1
</option>
<!--
This ensures that we don't wind up accidentally marking two options
(Unknown and the blank one) as selected.
-->
<option selected="selected" value="8">
2
</option>
</select>
This is what I am trying but in vain.
season_container = page_html.find_all("select", class_="current")
print(season_container.find_all('option', selected=True))
You can narrow your search by selecting using id
.
from bs4 import BeautifulSoup
html = """<select id="bySeason" tconst="tt0944947" class="current">
<!--
This ensures that we don't wind up accidentally marking two options
(Unknown and the blank one) as selected.
-->
<option value="1">
1
</option>
<!--
This ensures that we don't wind up accidentally marking two options
(Unknown and the blank one) as selected.
-->
<option selected="selected" value="8">
2
</option>
</select>
"""
soup = BeautifulSoup(html, "html.parser")
selected_value = soup.find("select", {"id":"bySeason"}).find("option",selected=True)
print(selected_value.get_text(strip=True))
print("-------")
print(selected_value["value"])
Output:
2
-------
8