Skip to content Skip to sidebar Skip to footer

How Can I Preserve
As Newlines With Lxml.html Text_content() Or Equivalent?

I want to preserve
tags as \n when extracting the text content from lxml elements. Example code: fragment = '
This is a text node.
This is another t

Solution 1:

Prepending an \n character to the tail of each <br /> element should give the result you're expecting:

>>> 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.\n\nAnd 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>'

Post a Comment for "How Can I Preserve
As Newlines With Lxml.html Text_content() Or Equivalent?"