How can I preserve
as newlines with lxml.html text_content() or equivalent?

前端 未结 1 470
清歌不尽
清歌不尽 2020-12-14 19:10

I want to preserve
tags as \\n when extracting the text content from lxml elements.

Example code:

fragment = \'&l

1条回答
  •  甜味超标
    2020-12-14 19:21

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

    >>> import lxml.html as html
    >>> fragment = '
    This is a text node.
    This is another text node.

    And a child element.Another child,
    with two text nodes
    ' >>> 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 '
    This is a text node.
    This is another text node.

    And a child element.Another child,
    with two text nodes
    '

    0 讨论(0)
提交回复
热议问题