How to get all direct children of a BeautifulSoup Tag? [closed]
Asked Answered
S

1

29

How to retrieve (not recursively) all children using BeautifulSoup (bs4) ?

<div class='body'><span>A</span><span><span>B</span></span><span>C</span></div>

I want to get blocks like this :

block1 : <span>A</span>
block2 : <span><span>B</span></span>
block3 : <span>C</span>

I'm doing this way :

for j in soup.find_all(True)[:1]:
            if isinstance(j, NavigableString):
                continue
            if isinstance(j, Tag):
                tags.append(j.name)
                # Get siblings
                for k in j.find_next_siblings():
                    # k is sibling of first element

Is there a cleaner way to do that?

Sodomy answered 31/12, 2017 at 19:49 Comment(0)
T
54

You can set the recursive argument to False if you want to select only direct descendants.
An example with the html you provided:

from bs4 import BeautifulSoup

html = "<div class='body'><span>A</span><span><span>B</span></span><span>C</span></div>"
soup = BeautifulSoup(html, "lxml") 
for j in soup.div.find_all(recursive=False):
    print(j)

<span>A</span>
<span><span>B</span></span>
<span>C</span>
Trainband answered 31/12, 2017 at 20:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.