亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

獲取 Beautfiulsoup div 類內容

獲取 Beautfiulsoup div 類內容

阿波羅的戰車 2023-10-31 16:06:12
我正在研究 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())


查看完整回答
反對 回復 2023-10-31
  • 1 回答
  • 0 關注
  • 137 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號