title: Equivalent to InnerHTML when using lxml.html to parse HTML
body: <p>I'm working on a script using lxml.html to parse web pages. I have done a fair bit of BeautifulSoup in my time but am now experimenting with lxml due to its speed.</p>
  
  <p>I would like to know what the most sensible way in the library is to do the equivalent of Javascript's InnerHtml - that is, to retrieve or set the complete contents of a tag.</p>
  
  <pre><code>&lt;body&gt;
  &lt;h1&gt;A title&lt;/h1&gt;
  &lt;p&gt;Some text&lt;/p&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>InnerHtml is therefore:</p>
  
  <pre><code>&lt;h1&gt;A title&lt;/h1&gt;
  &lt;p&gt;Some text&lt;/p&gt;
  </code></pre>
  
  <p>I can do it using hacks (converting to string/regexes etc) but I'm assuming that there is a correct way to do this using the library which I am missing due to unfamiliarity. Thanks for any help.</p>
  
  <p>EDIT: Thanks to pobk for showing me the way on this so quickly and effectively. For anyone trying the same, here is what I ended up with:</p>
  
  <pre><code>from lxml import html
  from cStringIO import StringIO
  t = html.parse(StringIO(
  """&lt;body&gt;
  &lt;h1&gt;A title&lt;/h1&gt;
  &lt;p&gt;Some text&lt;/p&gt;
  Untagged text
  &lt;p&gt;
  Unclosed p tag
  &lt;/body&gt;"""))
  root = t.getroot()
  body = root.body
  print (element.text or '') + ''.join([html.tostring(child) for child in body.iterdescendants()])
  </code></pre>
  
  <p>Note that the lxml.html parser will fix up the unclosed tag, so beware if this is a problem.</p>