我正在研究 beautifulsoup。我想访问 div 中的文本。我的代码如下。attack = atackersoup.findAll("div", {"class":"col-12 description"})我的输出如下<div class="col-12 description">
A denial of service vulnerability was identified that exists in Apache SpamAssassin before 3.4.2.
</div>我只想要文字。不显示 div 标签。
1 回答
慕侠2389804
TA贡献1719条经验 获得超6个赞
text要从标签中获取,请使用以下命令:
print(attack.text.strip())
输出:
A denial of service vulnerability was identified that exists in Apache SpamAssassin before 3.4.2.
这是完整的代码:
html = """
<div class="col-12 description">
A denial of service vulnerability was identified that exists in Apache SpamAssassin before 3.4.2.
</div>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html,'html5lib')
div = soup.find('div', class_ = "col-12 description")
print(div.text.strip())
由于您有一个元素列表,因此您应该循环遍历元素并打印文本,例如:
for div in attack:
print(div.text.strip())
添加回答
举报
0/150
提交
取消