Is there an equivalent of Beautiful Soup’s tag.renderContents() method in lxml?
I’ve tried using element.text, but that doesn’t render child tags, as well as ''.join(etree.tostring(child) for child in element), but that doesn’t render child text. The closest I’ve been able to find is etree.tostring(element), but that renders the opening and closing tags of element, which I do not want.
Is there another method I’m overlooking (or an alternative approach to accomplish this)?
You’re most of the way there with your original idea.
element.textgives you the first text child of the element, and your list comprehension gives you everything else. If you concatenate the two strings together, you get what you’re looking for:Ari.