相关疑难解决方法(0)

如何使用lxml.html text_content()或等效内容将<br>保留为换行符?

我想保留<br>标签,就像\n从lxml元素中提取文本内容一样.

示例代码:

fragment = '<div>This is a text node.<br/>This is another text node.<br/><br/><span>And a child element.</span><span>Another child,<br> with two text nodes</span></div>'

h = lxml.html.fromstring(fragment)
Run Code Online (Sandbox Code Playgroud)

输出:

> h.text_content()
'This is a text node.This is another text node.And a child element.Another child, with two text nodes'
Run Code Online (Sandbox Code Playgroud)

python lxml lxml.html

16
推荐指数
1
解决办法
4978
查看次数

标签 统计

lxml ×1

lxml.html ×1

python ×1