Navigating table by using th text
<p>I have the following table:</p>

<pre><code>&lt;table class="information"&gt;
  &lt;tr&gt; .... lots of rows with &lt;th&gt; and &lt;td&gt;&lt;/tr&gt;
  &lt;tr&gt;
   &lt;th&gt;Nationality&lt;/th&gt;
   &lt;td&gt;&lt;a href=".."&gt;Stackoverflowian&lt;/a&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
</code></pre>

<p>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.</p>

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

<p>Find the <code>th</code> tag, then get its <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/#next-sibling-and-previous-sibling" rel="nofollow">next sibling</a>:</p>

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

# Stackoverflowian
</code></pre>

<p>We need to do <code>next_sibling</code> twice because the first one will give the newline.</p>

python beautifulsoup