我正在研究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标记。
要从标记中获取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())