Suppose I have an HTML snippet like this:
<div>
Hello <strong>There</strong>
<div>I think <em>I am</em> feeing better!</div>
<div>Don't you?</div>
Yup!
</div>
What’s the best/most robust way to remove the surrounding root element, so it looks like this:
Hello <strong>There</strong>
<div>I think <em>I am</em> feeing better!</div>
<div>Don't you?</div>
Yup!
I’ve tried using lxml.html like this:
lxml.html.fromstring(fragment_string).drop_tag()
But that only gives me “Hello”, which I guess makes sense. Any better ideas?
This is a bit odd in lxml (or ElementTree). You’d have to do:
Note that lxml (and ElementTree) have no special way to represent a document except rooted with a single element, but
.drop_tag()would work like you want if that<div>wasn’t the root element.