title: Finding direct child of an element
body: <p>I'm writing a solution to test <a href="https://en.wikipedia.org/wiki/Wikipedia:Getting_to_Philosophy" rel="nofollow noreferrer">this</a> phenomenon in Python. I have most of the logic done, but there are many edge cases that arise when following links in Wikipedia articles. </p>
  
  <p>The problem I'm running into arises for a page like <a href="https://en.wikipedia.org/wiki/Sierra_Leone" rel="nofollow noreferrer">this</a> where the first <code>&lt;p&gt;</code> has multiple levels of child elements and the first <code>&lt;a&gt;</code> tag after the first set of parentheses needs to be extracted. In this case, (to extract <a href="https://en.wikipedia.org/wiki/West_Africa" rel="nofollow noreferrer">this link</a>), you have to skip over the parentheses, and then get to the very next anchor tag/href. In most articles, my algorithm can skip over the parentheses, but with the way that it looks for links in front of parentheses (or if they don't exist), it is finding the anchor tag in the wrong place. Specifically, here: <code>&lt;span style="font-size: small;"&gt;&lt;span id="coordinates"&gt;&lt;a href="/wiki/Geographic_coordinate_system" title="Geographic coordinate system"&gt;Coordinates&lt;/a&gt;</code></p>
  
  <p>The algorithm works by iterating through the elements in the first paragraph tag (in the main body of the article), stringifying each element iteratively, and first checking to see if it contains either an '(' or an '
  
  <p><strong>Is there any straight forward way to avoid embedded anchor tags and only take the first link that is a direct child of the first <code>&lt;p&gt;</code> ?</strong></p>
  
  <p>Below is the function with this code for reference:</p>
  
  <pre><code>**def getValidLink(self, currResponse):
          currRoot = BeautifulSoup(currResponse.text,"lxml")
          temp = currRoot.body.findAll('p')[0]
          parenOpened = False
          parenCompleted = False
          openCount = 0
          foundParen = False
          while temp.next:
              temp = temp.next
              curr = str(temp)
              if '(' in curr and str(type(temp)) == "&lt;class 'bs4.element.NavigableString'&gt;":
                  foundParen = True
                  break
              if '&lt;a' in curr and str(type(temp)) == "&lt;class 'bs4.element.Tag'&gt;":
                  link = temp
                  break
  
          temp = currRoot.body.findAll('p')[0]
          if foundParen:
              while temp.next and not parenCompleted:
                  temp = temp.next
                  curr = str(temp)
                  if '(' in curr:
                      openCount += 1
                      if parenOpened is False:
                          parenOpened = True
                  if ')' in curr and parenOpened and openCount &gt; 1:
                      openCount -= 1
                  elif ')' in curr and parenOpened and openCount == 1:
                      parenCompleted = True
              try:
                  return temp.findNext('a').attrs['href']
              except KeyError:
                  print "\nReached article with no main body!\n"
                  return None
          try:
              return str(link.attrs['href'])
          except KeyError:
              print "\nReached article with no main body\n"
              return None**
  </code></pre>