title: beautifulsoup: how to get indexes of elements in table headers
body: <p>I'm trying to extract the index of elements in the table headers, so that I can use the result to select the appropriate columns later on, in the body of the table. The columns vary in number but the ones I need remain constant in terms of their headings.</p>
  
  <p>So I'd like to know that,for example, 'third' is index[2] in the headers of a table thus &lsaquo;th>first&lsaquo;/th>&lsaquo;th>second&lsaquo;/th>&lsaquo;th>third&lsaquo;/th>&lsaquo;th>fourth&lsaquo;/th>&lsaquo;th>fifth&lsaquo;/th> 
  I can then selectively choose the relevant &lsaquo;td>s in the following rows by selecting the &lsaquo;td>'s index number.</p>
  
  <p>Here's my attempt:</p>
  
  <pre><code>#TRIAL TO GET INDEXES FROM TABLE HEADERS
  from bs4 import BeautifulSoup
  html = '&lt;table&gt;&lt;thead&gt;&lt;tr class="myClass"&gt;&lt;th&gt;A&lt;/th&gt;'
  '&lt;th&gt;B&lt;/th&gt;&lt;th&gt;C&lt;/th&gt;&lt;th&gt;D&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;/table&gt;'
  soup = BeautifulSoup(html)
  
  table = soup.find('table')
  
  for hRow in table.find_all('th'):
  hRow = hRow.index('A')
  print hRow  
  </code></pre>
  
  <p>Gives: </p>
  
  <blockquote>
    <p>ValueError: Tag.index: element not in tag</p>
  </blockquote>
  
  <p>Any thoughts?</p>