Extract data from an HTML tag which has specific attributes
<pre><code>&lt;tr bgcolor="#FFFFFF"&gt;
    &lt;td class="tablecontent" scope="row" rowspan="1"&gt;
        &lt;a href="http://netprofile-us2/netprofile/npIndex.do?cpyKey=80823"&gt;ZURICH AMERICAN INSURANCE COMPANY&lt;/a&gt;
    &lt;/td&gt;
    &lt;td class="tablecontent" scope="row" rowspan="1"&gt;
        FARMERS GROUP INC (14523)
    &lt;/td&gt;
    &lt;td class="tablecontent" scope="row"&gt;
        znaf
    &lt;/td&gt;
    &lt;td class="tablecontent" scope="row"&gt;
        anhm
    &lt;/td&gt;
&lt;/tr&gt;
</code></pre>

<p>I have an HTML document which contains multiple <code>tr</code> tags. I want to extract the <code>href</code> link from the first <code>td</code> and data from third <code>td</code> tag onwards under every <code>tr</code> tag. How can this be achieved?</p>

<p>You can find all <code>tr</code> elements, iterate over them, then do the context-specific searches for the inner <code>td</code> elements and get the first and the third:</p>

<pre><code>for tr in soup.find_all('tr'):
    cells = tr.find_all('td')
    if len(cells) &lt; 3:
        continue  # safety pillow

    link = cells[0].a['href']  # assuming every first td has an "a" element
    data = cells[2].get_text()

    print(link, data)
</code></pre>

<p>As a side note and depending what you are trying to accomplish in the HTML parsing, I usually find <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_html.html" rel="nofollow"><code>pandas.read_html()</code></a> a great and convenient way to parse HTML tables into <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html" rel="nofollow">dataframes</a> and work with the dataframes after, which are quite convenient data structures to work with.</p>

python python-2.7 beautifulsoup