获取 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 标签。


阿波罗的战车
浏览 109回答 1
1回答

慕侠2389804

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">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; A denial of service vulnerability was identified that exists in Apache SpamAssassin before 3.4.2.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>"""from bs4 import BeautifulSoupsoup = BeautifulSoup(html,'html5lib')div = soup.find('div', class_ = "col-12 description")print(div.text.strip())由于您有一个元素列表,因此您应该循环遍历元素并打印文本,例如:for div in attack:&nbsp; &nbsp; print(div.text.strip())
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python