How can one replace an element in lxml?

馋奶兔 提交于 2019-12-25 02:55:22

问题


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


回答1:


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

from  lxml import html
from lxml import etree

tree = html.fragment_fromstring( description )
etree.strip_elements(tree, 'br', with_tail=False)

print etree.tostring(tree,pretty_print=True)


来源:https://stackoverflow.com/questions/30515884/how-can-one-replace-an-element-in-lxml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!