While the accepted answer from phlou will work, there are simpler ways to remove tags without removing their tails.
If you want to delete a specific element, then the LXML method you are looking for is drop_tree.
:
. el.getparent(). Remove (el) ; drop_tree .
, lxml.etree.strip_elements lxml.html.etree.strip_elements withtails=False.
. , , . , with_tail False.
, :
>>> from lxml.html import fragment_fromstring, tostring
>>>
>>> html = fragment_fromstring('<a><b>Text</b>Text2</a>')
>>> for bad in html.xpath('.//b'):
... bad.drop_tag()
>>> tostring(html)
'<a>Text2</a>'
>>> from lxml.html import fragment_fromstring, tostring, etree
>>>
>>> html = fragment_fromstring('<a><b>Text</b>Text2</a>')
>>> etree.strip_elements(html, 'b', with_tail=False)
>>> tostring(html)
'<a>Text2</a>'