title: HTML Parsing using Python
body: <p>I need to parse a webpage and extract some values from it. So I created a python parser as follow:</p>
  
  <pre><code>from HTMLParser import HTMLParser
  class MyHTMLParser(HTMLParser):
      def handle_data(self, data):
          print "Data     :", data
  
  f=open("result.html","r")
  s=f.read()
  parser = MyHTMLParser()
  parser.feed(s)
  </code></pre>
  
  <p>The program reads html file and prints the data from it.</p>
  
  <p>I passed following result.html, here parser works fine</p>
  
  <pre><code>&lt;tr class='trmenu1'&gt;
  &lt;td&gt;Marks Obtained: &lt;/td&gt;&lt;td colspan=1&gt;75.67 Out of 100&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr class='trmenu1'&gt;
  &lt;td&gt;GATE Score: &lt;/td&gt;&lt;td colspan=1&gt;911&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr class='trmenu1'&gt;
  &lt;td&gt;All India Rank: &lt;/td&gt;&lt;td colspan=1&gt;34&lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>After passing the above html the output is:</p>
  
  <blockquote>
    <p>Data     : </p>
    
    <p>Data     : Marks Obtained:<br>
    Data     : 75.67 Out of 100  Data     :</p>
    
    <p>Data     : </p>
    
    <p>Data     : </p>
    
    <p>Data     : GATE Score:<br>
    Data     : 911<br>
    Data     :</p>
    
    <p>Data     : </p>
    
    <p>Data     : </p>
    
    <p>Data     : All India Rank:<br>
    Data     : 34 </p>
  </blockquote>
  
  <p>But the parser is supposed to read a larger file and the code mentioned above is small part of that large file. The file is too large to paste here. So I uploaded it at following link: <a href="http://www.mediafire.com/?dsgr1gdjvs59c7c" rel="nofollow">http://www.mediafire.com/?dsgr1gdjvs59c7c</a>
  When passed the larger file, parser doesn't read all the entries leaving some blank entries in output.
  Part of output is shown below:</p>
  
  <blockquote>
    <p>Data     : Syllabi  </p>
    
    <p>Data     : </p>
    
    <p>Data     : GATE Score  </p>
    
    <p>Data     : </p>
    
    <p>Data     : GATE Results  </p>
    
    <p>Data     :</p>
  </blockquote>
  
  <p>Observe the blank entry in the line below Gate Score which was 911 in previous output.</p>
  
  <p>The parser works fine with small file but not with the large file
  Why is this happening? I am using Python 2.7</p>