获取Beautfiulsoup div类内容

问题描述

我正在研究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())