182

I want to get all the <a> tags which are children of <li>:

<div>
<li class="test">
    <a>link1</a>
    <ul> 
       <li>  
          <a>link2</a> 
       </li>
    </ul>
</li>
</div>

I know how to find element with particular class like this:

soup.find("li", { "class" : "test" }) 

But I don't know how to find all <a> which are children of <li class=test> but not any others.

Like I want to select:

<a>link1</a>
daaawx
  • 3,273
  • 2
  • 17
  • 16
tej.tan
  • 4,067
  • 6
  • 28
  • 29

7 Answers7

211

Try this

li = soup.find('li', {'class': 'test'})
children = li.findChildren("a" , recursive=False)
for child in children:
    print(child)
ofir_aghai
  • 3,017
  • 1
  • 37
  • 43
cerberos
  • 7,705
  • 5
  • 41
  • 43
150

There's a super small section in the DOCs that shows how to find/find_all direct children.

https://www.crummy.com/software/BeautifulSoup/bs4/doc/#the-recursive-argument

In your case as you want link1 which is first direct child:

# for only first direct child
soup.find("li", { "class" : "test" }).find("a", recursive=False)

If you want all direct children:

# for all direct children
soup.find("li", { "class" : "test" }).findAll("a", recursive=False)
David A
  • 415
  • 1
  • 4
  • 13
strider
  • 5,674
  • 4
  • 24
  • 29
22

Perhaps you want to do

soup.find("li", { "class" : "test" }).find('a')
Bemmu
  • 17,849
  • 16
  • 76
  • 93
  • This does not answer the question, but it was what I was looking for. – Pro Q Oct 13 '20 at 20:14