title: How do you parse a poorly formatted HTML file?
body: <p>I have to parse a series of web pages in order to import data into an application. Each type of web page provides the same kind of data. The problem is that the HTML of each page is different, so the location of the data varies. Another problem is that the HTML code is poorly formatted, making it impossible to use a XML-like parser. </p>
  
  <p>So far, the best strategy I can think of, is to define a template for each kind of page, like:</p>
  
  <p>Template A:</p>
  
  <pre><code>&lt;html&gt;
  ...
    &lt;tr&gt;&lt;td&gt;Table column that is missing a td 
        &lt;td&gt; Another table column&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;$data_item_1$&lt;/td&gt;
  ...
  &lt;/html&gt;
  </code></pre>
  
  <p>Template B:</p>
  
  <pre><code>&lt;html&gt;
  ...
    &lt;ul&gt;&lt;li&gt;Yet another poorly formatted page &lt;li&gt;$data_item_1$&lt;/td&gt;&lt;/tr&gt;
  ...
  &lt;/html&gt;
  </code></pre>
  
  <p>This way I would only need one single parser for all the pages, that would compare each page with its template and retrieving the <code>$data_item_1$</code>, <code>$data_item_2$</code>, etc.  Still, it is going to be a lot of work.  Can you think of any simpler solution? Any library that can help?</p>
  
  <p>Thanks</p>