How to find children of nodes using BeautifulSoup


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

<li class="test">

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:

5/17/2019 8:52:03 PM

Accepted Answer

Try this

li = soup.find('li', {'class': 'text'})
children = li.findChildren("a" , recursive=False)
for child in children:
    print child
7/28/2018 3:44:29 PM

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

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)

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow