【发布时间】:2020-04-29 05:18:54
【问题描述】:
我正在使用 lxml 提取与 html 标签关联的文本,但它也将 括号中的所有内容视为标签并将其丢弃。有什么方法可以保留文本中除标准 html 标签之外的所有内容吗?
from lxml import html
data="<EXPE>(i)<i>you</i>"
print(html.fragment_fromstring(data).text_content())
这给出了输出
'(i)you'
但期望的输出是
<Expe>(i)you
我尝试了以下美丽汤:
from bs4 import BeautifulSoup
soup = BeautifulSoup(data, 'html.parser')
text = soup.find_all(text=True)
print(text)
这也丢弃了
'<Expe>'
在它的输出中。我在 中有很多不是 html 标签的文本。那么有没有办法不省略它们?
【问题讨论】:
标签: python parsing beautifulsoup lxml