我正在尝试浏览BeautifulSoup4中的这个下拉列表,并且找不到BS4功能以在正确的位置插入“选中”。该列表如下所示:
<select name="sport" id="sport" onchange="mask('Processing'); changeSports(this.value);">
<option value="">Select Sport</option>
<option value="MBA" >Baseball</option>
<option value="MBB" >Men's Basketball</option>
<option value="MFB" >Football</option>
<option value="MIH" >Men's Ice Hockey</option>
<option value="MLA" >Men's Lacrosse</option>
<option value="MSO" >Men's Soccer</option>
<option value="MTE" >Men's Tennis</option>
<option value="MVB" >Men's Volleyball</option>
<option value="WBB" >Women's Basketball</option>
<option value="WBW" >Women's Bowling</option>
<option value="WFH" >Field Hockey</option>
<option value="WIH" >Women's Ice Hockey</option>
<option value="WLA" >Women's Lacrosse</option>
<option value="WSB" selected>Softball</option>
<option value="WSO" >Women's Soccer</option>
<option value="WSV" >Women's Beach Volleyball</option>
<option value="WTE" >Women's Tennis</option>
<option value="WVB" >Women's Volleyball</option>
</select>
我一直试图将“已选择”插入
<option value="WSB" >Softball</option>
使用这个python代码:
from bs4 import BeautifulSoup,NavigableString
import requests
headers = {'User-Agent': 'Mozilla/5.0'}
url = 'http://stats.ncaa.org/rankings/ranking_summary'
page = requests.get(url,headers=headers)
soup = BeautifulSoup(page.content, "html.parser")
sport = soup.find(value="WSB")
sport.insert(0,"selected")
print(sport)
但这会产生结果:
<option value="WSB">selectedSoftball</option>
我真的不太了解HTML,所以我很难找到寻找解决方案的地方,任何建议都会非常感激。
答案 0 :(得分:0)
在BeautifulSoup4中,属性的存储方式与字典类似。要修改selected
的{{1}}属性,请使用<option>
。