Extracting string from an HTML table from a given tab using Python
<p>I need to extract a string value from the HTML table below. I want to loop through the table from a particular tab, and copy the results horizontally into the command line or some file.</p>

<p>I am pasting only one row of information here. 
This table gets updated based on the changes happening on the Gerrits.
The result that I want is all the Gerrit number under a new tab
For example, if I want to get the Gerrit list from the approval queue, the values should display as shown in the image below.</p>

<p><img src="http://i.stack.imgur.com/HYjWV.png" alt="dashboard_image"></p>

<p>7897423, 2423343, 34242342, 34234, 57575675</p>

<pre><code>&lt;ul&gt;
    &lt;li&gt;&lt;a href="#tab1"&gt;&lt;span&gt;Review Queue&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="#tab2"&gt;&lt;span&gt;Approval Queue&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="#tab3"&gt;&lt;span&gt;Verification Queue&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="#tab4"&gt;&lt;span&gt;Merge Queue&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="#tab5"&gt;&lt;span&gt;Open Queue&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="#tab6"&gt;&lt;span&gt;Failed verification&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div id="tab1"&gt;
    &lt;h1&gt;Review Queue&lt;/h1&gt;
    &lt;table class="tablesorter" id="dashboardTable"&gt;
        &lt;thead&gt;
            &lt;tr&gt;
                &lt;th&gt;&lt;/th&gt;
                &lt;th&gt;Gerrit&lt;/th&gt;
                &lt;th&gt;Owner&lt;/th&gt;
                &lt;th&gt;CR(s)&lt;/th&gt;
                &lt;th&gt;Project&lt;/th&gt;
                &lt;th&gt;Dev Branch/PL&lt;/th&gt;
                &lt;th&gt;Subject&lt;/th&gt;
                &lt;th&gt;Status&lt;/th&gt;
                &lt;th&gt;Days in Queue&lt;/th&gt;
            &lt;/tr&gt;
        &lt;/thead&gt;
        &lt;tbody&gt;
            &lt;tr&gt;
                &lt;td&gt;&lt;input type="checkbox" /&gt;&lt;/td&gt;
                &lt;td&gt; &lt;a href="https://example/1696771"&gt;1696771&lt;/a&gt; &lt;/td&gt;
                &lt;td&gt; &lt;a href="http://people.theng/People?id=ponga"&gt;ponga&lt;/a&gt; &lt;/td&gt;
                &lt;td&gt; &lt;a href="http://chong/CR/1055680"&gt;1055680&lt;/a&gt; &lt;/td&gt;
                &lt;td&gt;platform/hardware/kiosk/&lt;/td&gt;
                &lt;td&gt; hidden-userspace.aix.2.0.dev &lt;/td&gt;
                &lt;td&gt;display: information regarding display&lt;/td&gt;
                &lt;td&gt; some info here &lt;/td&gt;
                &lt;td&gt; 2 &lt;/td&gt;
            &lt;/tr&gt;
</code></pre>

<p>What stops you from leveraging BeautifulSoup for this?</p>

<p>Lets say you have already read the html (using sgmllib or any other library) into a string variable named html_contents. 
Since you are not mentioning which column you want to get data from, I am extracting the gerrit number column.</p>

<p>You can simply do:</p>

<pre><code>from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')

for tr in soup.tbody:
  if tr.name == 'tr':
    print tr.contents[3].contents[1].string
</code></pre>

<p>Above you can loop in all the tr tags inside the tbody and (assuming all the td tags contained inside the tr have the same structure) their value is extracted, in this case the value of the a tag inside.</p>

<p><a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/#quick-start" rel="nofollow">Read the quick start</a>, it will make your life easier on parsing HTML documents.</p>

python html parsing html-table