How to find children of nodes using BeautifulSoup How to find children of nodes using BeautifulSoup python python

How to find children of nodes using BeautifulSoup


Try this

li = soup.find('li', {'class': 'text'})children = li.findChildren("a" , recursive=False)for child in children:    print(child)


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 childsoup.find("li", { "class" : "test" }).find("a", recursive=False)

If you want all direct children:

# for all direct childrensoup.find("li", { "class" : "test" }).findAll("a", recursive=False)


Perhaps you want to do

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