猿问

如何从html页面中提取没有标记标签的文本内容?

我只想从 html 页面中提取不包括标记的文本。我怎样才能在 python(最好)或 java 脚本中实现这一点?


对于以下代码:


<div id = #one>

 OneDivision

 <div id = #two>TwoDivision</div>

 <span>SpanElement</span>

</div>

我的输出应该是: OneDivision TwoDivision SpanElement


千万里不及你
浏览 542回答 3
3回答

HUWWW

超级简单!在 Javascript 中,使用textContent. 看下面的代码console.log(document.getElementById("one").textContent);<div id = "one">&nbsp;OneDivision&nbsp;<div id = "two">TwoDivision</div>&nbsp;<span>SpanElement</span></div>

宝慕林4294392

from bs4 import BeautifulSouphtml = '<div id = #one>OneDivision<div id = #two>TwoDivision</div><span>SpanElement</span></div>'soup = BeautifulSoup(html,"lxml")print(soup.get_text(separator=' '))输出'OneDivision TwoDivision SpanElement'

一只甜甜圈

html_doc = BeautifulSoup(html, 'lxml').bodyif html_doc is None:&nbsp; &nbsp; return Nonefor tag in html_doc.select('script'):&nbsp; &nbsp; tag.decompose()for tag in html_doc.select('style'):&nbsp; &nbsp; tag.decompose()text = html_doc.get_text(separator='\n')
随时随地看视频慕课网APP

相关分类

Python
我要回答