title: Python - Getting the first child of the parent element with BeautifulSoup4
body: <p>I have the following HTML:</p>
  
  <pre><code>&lt;table id="mytable"&gt;
      &lt;tr role="row"&gt;
          &lt;td&gt;abc1&lt;/td&gt;
          &lt;td&gt;abc2&lt;/td&gt;
          &lt;td&gt;abc3&lt;/td&gt;
          &lt;td class="hm"&gt;Data1&lt;/td&gt;
          &lt;td&gt;Data2&lt;/td&gt;
          &lt;td class="hm"&gt;no&lt;/td&gt;
          &lt;td class="hx"&gt;yes&lt;/td&gt;
          &lt;td class="hm"&gt;Updated&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr role="row"&gt;
          &lt;td&gt;def1&lt;/td&gt;
          &lt;td&gt;def2&lt;/td&gt;
          &lt;td&gt;def3&lt;/td&gt;
          &lt;td class="hm"&gt;Data3&lt;/td&gt;
          &lt;td&gt;Data4&lt;/td&gt;
          &lt;td class="hm"&gt;no&lt;/td&gt;
          &lt;td class="hx"&gt;no&lt;/td&gt;
          &lt;td class="hm"&gt;Updated&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr role="row"&gt;
          &lt;td&gt;hij1&lt;/td&gt;
          &lt;td&gt;hij2&lt;/td&gt;
          &lt;td&gt;hij3&lt;/td&gt;
          &lt;td class="hm"&gt;Data5&lt;/td&gt;
          &lt;td&gt;Data6&lt;/td&gt;
          &lt;td class="hm"&gt;no&lt;/td&gt;
          &lt;td class="hx"&gt;no&lt;/td&gt;
          &lt;td class="hm"&gt;Updated&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr role="row"&gt;
          &lt;td&gt;klm1&lt;/td&gt;
          &lt;td&gt;klm2&lt;/td&gt;
          &lt;td&gt;klm3&lt;/td&gt;
          &lt;td class="hm"&gt;Data7&lt;/td&gt;
          &lt;td&gt;Data8&lt;/td&gt;
          &lt;td class="hm"&gt;no&lt;/td&gt;
          &lt;td class="hx"&gt;yes&lt;/td&gt;
          &lt;td class="hm"&gt;Updated&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>There are multiples of these <code>&lt;tr&gt;</code> tags so I am doing the following to find one where there is a child <code>&lt;td&gt;</code> tag with <code>class="hx"</code> and a text of <code>yes</code>:</p>
  
  <pre><code>if (Soup.find('table', {'id' : 'mytable'}).find('td', text='yes', attrs={'class' : 'hx'})):
      print "Found 'yes'"
  </code></pre>
  
  <p>But I am interested in the first <code>&lt;td&gt;</code> tag in the <code>&lt;tr&gt;</code> that is found. I tried doing the following, but it doesn't seem to work:</p>
  
  <pre><code>print Soup.find('table', {'id' : 'mytable'}).find('td', text='yes', attrs={'class' : 'hx'}).parent.find('td')[0].text
  </code></pre>
  
  <p>Alternatively, the following works but it seems like a really drawn out way of doing this, especially if I am not sure how many <code>&lt;td&gt;</code> tags will be present between the two:</p>
  
  <pre><code>print Soup.find('table', {'id' : 'mytable'}).find('td', text='yes', attrs={'class' : 'hx'}).previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.text
  </code></pre>
  
  <p>Is there a better way of doing this? Am I missing something?</p>