Question
Asked By – tej.tan
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>
Now we will see solution for issue: How to find children of nodes using BeautifulSoup
Answer
Try this
li = soup.find('li', {'class': 'text'})
children = li.findChildren("a" , recursive=False)
for child in children:
print(child)
This question is answered By – cerberos
This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0