title: How to parse malformed HTML in python, using standard libraries
body: <p>There are so many <a href="http://docs.python.org/library/markup.html" rel="noreferrer">html and xml libraries built into python</a>, that it's hard to believe there's no support for real-world HTML parsing.</p>
  
  <p>I've found plenty of great third-party libraries for this task, but this question is about the python standard library.</p>
  
  <p>Requirements:</p>
  
  <ul>
  <li>Use only Python standard library components (any 2.x version)</li>
  <li>DOM support</li>
  <li>Handle HTML entities (<code>&amp;nbsp;</code>)</li>
  <li>Handle partial documents (like: <code>Hello, &lt;i>World&lt;/i>!</code>)</li>
  </ul>
  
  <p>Bonus points:</p>
  
  <ul>
  <li>XPATH support</li>
  <li>Handle unclosed/malformed tags. (<code>&lt;big>does anyone here know &lt;html ???</code></li>
  </ul>
  
  <hr>
  
  <p>Here's my 90% solution, as requested. This works for the limited set of HTML I've tried, but as everyone can plainly see, this isn't exactly robust. Since I did this by staring at the docs for 15 minutes and one line of code, I thought I would be able to consult the stackoverflow community for a similar but better solution...</p>
  
  <pre><code>from xml.etree.ElementTree import fromstring
  DOM = fromstring("&lt;html&gt;%s&lt;/html&gt;" % html.replace('&amp;nbsp;', '&amp;#160;'))
  </code></pre>