157

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
  • 2,798
  • 2
  • 14
  • 15
tej.tan
  • 3,715
  • 6
  • 26
  • 29

7 Answers7

179

Try this

li = soup.find('li', {'class': 'text'})
children = li.findChildren("a" , recursive=False)
for child in children:
    print(child)
malat
  • 11,258
  • 13
  • 77
  • 141
cerberos
  • 7,165
  • 4
  • 38
  • 43
137

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
  • 405
  • 1
  • 4
  • 13
strider
  • 5,114
  • 3
  • 22
  • 29
19

Perhaps you want to do

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