Я думаю, что strip_tags
и strip_elements
- это то, что вы хотите в каждом случае.Например, этот скрипт:
from lxml import etree
text = "<x>hello, <z>keep me</z> and <y>ignore me</y>, and here's some <y>more</y> text</x>"
tree = etree.fromstring(text)
print etree.tostring(tree, pretty_print=True)
# Remove the <z> tags, but keep their contents:
etree.strip_tags(tree, 'z')
print '-' * 72
print etree.tostring(tree, pretty_print=True)
# Remove all the <y> tags including their contents:
etree.strip_elements(tree, 'y', with_tail=False)
print '-' * 72
print etree.tostring(tree, pretty_print=True)
... производит следующий вывод:
<x>hello, <z>keep me</z> and <y>ignore me</y>, and
here's some <y>more</y> text</x>
------------------------------------------------------------------------
<x>hello, keep me and <y>ignore me</y>, and
here's some <y>more</y> text</x>
------------------------------------------------------------------------
<x>hello, keep me and , and
here's some text</x>