Search code examples
pythonlxmlelementtreelxml.html

How can one replace an element in lxml?


I have a text that I get (data entered by users of CRM) web service, which returns a "terrifying format". I am filtering with python before using the data, but when it comes to removing line breaks (br) removed me also the texts. The code is as follows:

description = '''
<div id="highlight" class="section">
    <p>
        text...............
    </p>
    <br>
    <h1>TITLE</h1>
    <p>Multiple text
        <br>&nbsp;
    </p>
    <ul>
        <li>bad layer....</li>
    </ul>
    <p>
        <br>subTitle
    </p>
    <p>&nbsp;</p>
    <p style="text-align: center;">
        <br>Text1
        <br>Text2
        <br>Text3
        <br>Text4
        <br>Text5
        <br>Text6
    </p>
    <p style="text-align: center;">
        <strong>small title</strong>
        <br>Text small</p>
    <p style="text-align: center;">
        <strong>highlighted text</strong>
        <br>
        <br><strong>Text1</strong>
        <br>Text2
        <br>Text3
        <br>Text4
    </p>
    <p style="text-align: center;">
        <strong>small text</strong>
        <br>Text1
        <br>Text2
    </p>
    <p style="text-align: center;">
        <strong>small text</strong>
        <br>description
    </p>
    <p style="text-align: center;">
        <br>&nbsp;</p>
    <p><strong>description two</strong></p>
    <p>
        <br>&nbsp;</p>
</div>
'''

tree = html.fragment_fromstring( description )

for element in tree.xpath('//br'):
    #element.getparent().remove(element)
    print element.text
    print element.getparent().getchildren()
    #print element
    #print element.getparent()
    #print element.getchildren()
    #print element.getnext()
    #print '--------------------------------'

I have tried to remove the br with element.getparent().remove(element), but also deletes the text, I did tests to see if the texts belong to any node, but not so.

I've thought about changing the br by li, making the p with stylo in ul, but I can't think as do it, something like this (the previous text lame):

..........
..........
<ul>
    <li>Text1</li>
    <li>Text2</li>
    <li>Text3</li>
    <li>Text4</li>
    <li>Text5</li>
    <li>Text6</li>
</ul>
<ul>
    <li><strong>small title</strong></li>
    <li>Text small</li></ul>
<ul>
    <li><strong>highlighted text</strong></li>
    <li><strong>Text1</strong></li>
    <li>Text2</li>
    <li>Text3</li>
    <li>Text4</li>
</ul>
<ul>
    <li><strong>small text</strong></li>
    <li>Text1</li>
    <li>Text2</li>
</ul>
<ul>
    <li><strong>small text</strong></li>
    <li>description</li>
</ul>
<ul>
    <li>&nbsp;</li></ul>
........

I can't think as take texts, because I thought that just choosing the xpath of the node p with style and its value, creating nodes children of li and a parent ul, eliminated p.

Is possible? Thanks

Regards


Solution

  • You can use lxml.etree.strip_elements, like so:

    import lxml.etree
    import lxml.html
    
    tree = lxml.html.fragment_fromstring(description)
    lxml.etree.strip_elements(tree, 'br', with_tail=False)
        
    print(lxml.etree.tostring(tree, pretty_print=True))