我正在研究 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
提交
取消