title: Use BeautifulSoup to fetch rows by header
body: <p>I have an html structure like this one:</p>
  
  <pre><code>&lt;table class="info" id="stats"&gt;
   &lt;tbody&gt;
    &lt;tr&gt;
     &lt;th&gt; Brand &lt;/th&gt;
     &lt;td&gt; 2 Guys Smoke Shop &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
     &lt;th&gt; Blend Type &lt;/th&gt;
     &lt;td&gt; Aromatic &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
     &lt;th&gt; Contents &lt;/th&gt;
     &lt;td&gt; Black Cavendish, Virginia &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
     &lt;th&gt; Flavoring &lt;/th&gt;
     &lt;td&gt; Other / Misc &lt;/td&gt;
    &lt;/tr&gt;
   &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>Those attributes are not always present, sometimes I can have only <code>Brand</code>, other cases <code>Brand</code> and <code>Flavoring</code>.</p>
  
  <p>To scrap this I did a code like this:</p>
  
  <pre><code>BlendInfo = namedtuple('BlendInfo', ['brand', 'type', 'contents', 'flavoring'])
  stats_rows =  soup.find('table', id='stats').find_all('tr')
  bi = BlendInfo(brand      = stats_rows[1].td.get_text(),
                 type       = stats_rows[2].td.get_text(),
                 contents   = stats_rows[3].td.get_text(),
                 flavoring  = stats_rows[4].td.get_text())
  </code></pre>
  
  <p>But as expected it fails with index out bounds (or get really messed up) when the table ordering is different (type before brand) or some of the rows are missing (no contents).</p>
  
  <p>Is there any better approach to something like:</p>
  
  <p><em>Give me the data from row with header with string 'brand'</em></p>