[Python] Why might lmxl insert <p> tags when parsing my xhtml file?

Discussão em 'Python' iniciado por Stack, Março 4, 2025.

  1. Stack

    Stack Membro Participativo

    I'm very new to working with HTML/XML so forgive me if I don't include something important.

    I have an xhtml file that I'm trying to parse using the Python lmxl library. The goal is to place the result into a dita (xml) file, but the output has several <p> tags that were not there before. Could anybody give me insight as to why this might be happening?

    Code:

    from lxml import etree as et

    parser = et.XMLPullParser(target=target)

    print(et.tostring(element, encoding="utf-8"))
    >>> <dd> T &gt; 85C : 10ms 85 =&gt; T &gt 30C : 100ms T &lt; 30 : 1s</dd>

    parser.feed(et.tostring(element, encoding="utf-8"))

    parser.close()


    Expected output:

    print(et.tostring(target.body, encoding="utf-8")))
    >>> <body>T &gt; 85C : 10ms 85 =&gt; T &gt 30C : 100ms T &lt; 30 : 1s</body>


    Actual output:

    print(et.tostring(target.body, encoding="utf-8")))
    >>> <body><p> T </p><p>&gt;</p><p> 85C : 10ms 85 =</p><p>&gt;</p><p> T </p><p>&gt</p><p> 30C : 100ms T </p><p>&lt;</p><p> 30 : 1s </p></body>

    Continue reading...

Compartilhe esta Página