如何使用lxml.html text_content()或同等功能保留为换行符?

如何使用lxml.html text_content()或同等功能保留为换行符?,第1张

如何 使用lxml.html text_content()或同等功能保留为换行符?

n
在每个
<br />
元素的末尾添加一个字符应该可以得到预期的结果:

>>> import lxml.html as html>>> 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>'>>> doc = html.document_fromstring(fragment)>>> for br in doc.xpath("*//br"):        br.tail = "n" + br.tail if br.tail else "n">>> doc.text_content()'This is a text node.nThis is another text node.nnAnd a child element.Another child,n with two text nodes'>>> 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>'


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5630983.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存