How to find children of nodes using BeautifulSoup
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Orient Looping
--
Chapters
00:00 How To Find Children Of Nodes Using Beautifulsoup
00:20 Accepted Answer Score 217
00:31 Answer 2 Score 150
00:56 Answer 3 Score 24
01:05 Answer 4 Score 18
01:34 Answer 5 Score 16
01:51 Thank you
--
Full question
https://stackoverflow.com/questions/6287...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #html #beautifulsoup
#avk47
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Orient Looping
--
Chapters
00:00 How To Find Children Of Nodes Using Beautifulsoup
00:20 Accepted Answer Score 217
00:31 Answer 2 Score 150
00:56 Answer 3 Score 24
01:05 Answer 4 Score 18
01:34 Answer 5 Score 16
01:51 Thank you
--
Full question
https://stackoverflow.com/questions/6287...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #html #beautifulsoup
#avk47
ACCEPTED ANSWER
Score 220
Try this
li = soup.find('li', {'class': 'test'})
children = li.findChildren("a" , recursive=False)
for child in children:
print(child)
ANSWER 2
Score 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)
ANSWER 3
Score 24
Perhaps you want to do
soup.find("li", { "class" : "test" }).find('a')
ANSWER 4
Score 16
try this:
li = soup.find("li", { "class" : "test" })
children = li.find_all("a") # returns a list of all <a> children of li
other reminders:
The find method only gets the first occurring child element. The find_all method gets all descendant elements and are stored in a list.