bjornasm bjornasm - 1 year ago 77
Python Question

Navigating table by using th text

I have the following table:

<table class="information">
<tr> .... lots of rows with <th> and <td></tr>
<td><a href="..">Stackoverflowian</a></td>

I want to find the text in inside the td tags under the th with "nationality" in it. How should I navigate there? I'm using Beautifulsoup and Python.

Added that there is lots of th and td tags above this that, to underline that it isnt sufficient to just find first th

Answer Source

Find the th tag, then get its next sibling:

soup = BeautifulSoup(html)
ths = soup.find_all('th')
for th in ths:
    if th.text == "Nationality":
        print th.next_sibling.next_sibling.text

# Stackoverflowian

We need to do next_sibling twice because the first one will give the newline.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download