In Python, how do I remove the "root" tag in an HTML snippet?

Posted by Chung Wu on Stack Overflow See other posts from Stack Overflow or by Chung Wu
Published on 2010-06-09T04:13:54Z Indexed on 2010/06/09 4:22 UTC
Read the original article Hit count: 225

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about python

Related posts about html