获取 Beautifulsoup div class 内容
Get Beautfiulsoup div class content
我正在研究 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())
我正在研究 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())