title: Scrape HTML table with standard Python HTMLParser
body: <p>I'm looking to scrape data from an HTML table using only the standard Python HTML Parser. I need to stick with stock tools because the code will be widely distributed and I cannot take the time to support those who need to install BeautifulSoup, lxlml, etc.</p>
  
  <p>For example, the HTML code:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>  &lt;table id="indexlist"&gt;
     &lt;tbody&gt;
       &lt;tr class="indexhead"&gt;
         &lt;th class="indexcolicon"&gt;
           &lt;img src="/icons/blank.gif" alt="[ICO]"&gt;
         &lt;/th&gt;
         &lt;th class="indexcolname"&gt;
           &lt;a href="?C=N;O=D"&gt;Name
           &lt;/a&gt;
         &lt;/th&gt;
         &lt;th class="indexcollastmod"&gt;
           &lt;a href="?C=M;O=A"&gt;Last modified
           &lt;/a&gt;
         &lt;/th&gt;
         &lt;th class="indexcolsize"&gt;
           &lt;a href="?C=S;O=A"&gt;Size
           &lt;/a&gt;
         &lt;/th&gt;
       &lt;/tr&gt;
     &lt;tr class="parent"&gt;
       &lt;td class="indexcolicon"&gt;
         &lt;a href="/pub/DATASETS/nsidc0081_nrt_nasateam_seaice/browse/"&gt;
           &lt;img src="/icons/back.gif" alt="[PARENTDIR]"&gt;
         &lt;/a&gt;
       &lt;/td&gt;
       &lt;td class="indexcolname"&gt;
         &lt;a href="/pub/DATASETS/nsidc0081_nrt_nasateam_seaice/browse/"&gt;Parent Directory
         &lt;/a&gt;
       &lt;/td&gt;
       &lt;td class="indexcollastmod"&gt;&amp;nbsp;
       &lt;/td&gt;
       &lt;td class="indexcolsize"&gt;  - 
       &lt;/td&gt;
       &lt;/tr&gt;
     &lt;tr class="odd"&gt;
       &lt;td class="indexcolicon"&gt;
         &lt;a href="nt_20150101_f17_nrt_n.png"&gt;
           &lt;img src="/icons/image2.gif" alt="[IMG]"&gt;
         &lt;/a&gt;
       &lt;/td&gt;
       &lt;td class="indexcolname"&gt;
         &lt;a href="nt_20150101_f17_nrt_n.png"&gt;
           nt_20150101_f17_nrt_n.png
         &lt;/a&gt;
       &lt;/td&gt;
       &lt;td class="indexcollastmod"&gt;
         2015-03-10 11:25  
       &lt;/td&gt;
       &lt;td class="indexcolsize"&gt; 56K
       &lt;/td&gt;
     &lt;/tr&gt;
     &lt;tr class="even"&gt;
       &lt;td class="indexcolicon"&gt;
       &lt;a href="nt_20150102_f17_nrt_n.png"&gt;
         &lt;img src="/icons/image2.gif" alt="[IMG]"&gt;
         &lt;/a&gt;
       &lt;/td&gt;
       &lt;td class="indexcolname"&gt;
         &lt;a href="nt_20150102_f17_nrt_n.png"&gt;
           nt_20150102_f17_nrt_n.png
         &lt;/a&gt;
       &lt;/td&gt;
  .
  .
  .</code></pre>
  </div>
  </div>
  </p>
  
  <p>I want to be able to extract the 'data' within this table. More specifically, the data would be all attribute values ending in *.png. These share the same name as the data within with the table. I do not want to explicitly declare that I'm wanting to scrape the *.png files because I would like to use this code in different directories, which will have different file formats. I've tried some code that tries to extract the value of all attributes with the name 'href', but this also returns numerous other attributes within the body of the HTML. Scraping only the data also returns some instances that fall outside of the table. For example:</p>
  
  <pre><code>class MyHTMLParser(HTMLParser):
  def __init__(self):
      HTMLParser.__init__(self)
      self.inLink = False
      self.dataArray = []
  
  def handle_starttag(self, tag, attrs):
      self.inLink = False
      if tag == 'a':
          for name, value in attrs:
              if name == 'href':
                  self.inLink = True
                  self.lasttag = tag
  
  def handle_data(self, data):
      if self.lasttag == 'a' and self.inLink and data.strip():
          self.dataArray.append(data)
  </code></pre>
  
  <p>However, this returns the following:</p>
  
  <pre><code>   nt_20170119_f18_nrt_n.png
     nt_20170120_f18_nrt_n.png
     nt_20170121_f18_nrt_n.png
     nt_20170122_f18_nrt_n.png
     Home
      | 
     Contact Us
  </code></pre>
  
  <p>since there are also a few 'a' tags that live outside the HTML table. Does someone have a method to extract the data or href values from within the table using standard HTML parsing methods?</p>