title: Using BeautifulSoup To Extract Specific TD Table Elements Text?
body: <p>I trying to extract IP Addresses from a autogenerated HTML table using the BeautifulSoup library and im having a little trouble.</p>
  
  <p>The HTML is structured like so:</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
      &lt;table class="mainTable"&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;IP&lt;/th&gt;
              &lt;th&gt;Country&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;a href="hello.html"&gt;127.0.0.1&lt;a&gt;&lt;/td&gt;
              &lt;td&gt;&lt;img src="uk.gif" /&gt;&lt;a href="uk.com"&gt;uk&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;a href="hello.html"&gt;192.168.0.1&lt;a&gt;&lt;/td&gt;
              &lt;td&gt;&lt;img src="uk.gif" /&gt;&lt;a href="us.com"&gt;us&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;a href="hello.html"&gt;255.255.255.0&lt;a&gt;&lt;/td&gt;
              &lt;td&gt;&lt;img src="uk.gif" /&gt;&lt;a href="br.com"&gt;br&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>
  </p>
  
  <p>The small code below extracts the text from the two td rows but i only need the IP data, not the IP and Country data:</p>
  
  <pre><code>from bs4 import BeautifulSoup
  soup = BeautifulSoup(open("data.htm"))
  
  table = soup.find('table', {'class': 'mainTable'})
  for row in table.findAll("a"):
  print(row.text)
  </code></pre>
  
  <p>this outputs:</p>
  
  <pre><code>127.0.0.1
  uk
  192.168.0.1
  us
  255.255.255.0
  br
  </code></pre>
  
  <p>What i need is the IP <code>table.tbody.tr.td.a</code> elements text and not the country <code>table.tbody.tr.td.img.a</code> elements.</p>
  
  <p>Are there any experienced users of BeautifulSoup who would have any inkling on how to to this selection and extraction.</p>
  
  <p>Thanks.</p>