2 回答
TA贡献1794条经验 获得超8个赞
用 .find(text=True)
前任:
from bs4 import BeautifulSoup
html = """<span class="age">
Ages 15
<span class="loc" id="loc_loads1">
</span>
<script>
getCurrentLocationVal("loc_loads1",29.45218856,59.38139268,1);
</script>
</span>"""
soup = BeautifulSoup(html, "html.parser")
print(soup.find("span", {"class": "age"}).find(text=True).strip())
输出:
Ages 15
TA贡献1982条经验 获得超2个赞
迟到的答案,但为了将来参考,您还可以使用分解()从 中删除所有script元素html,即:
soup = BeautifulSoup(html, "html.parser")
# remove script and style elements
for script in soup(["script", "style"]):
script.decompose()
print(soup.find("span", {"class": "age"}).text.strip())
# Ages 15
添加回答
举报