Skip to content Skip to sidebar Skip to footer

Find Next Siblings Until A Certain One Using Beautifulsoup

The webpage is something like this:

section1

article

article

article

section2

&l

Solution 1:

I think you can do something like this:

for section in soup.findAll('h2'):
    nextNode = section
    whileTrue:
        nextNode = nextNode.nextSibling
        try:
            tag_name = nextNode.name
        except AttributeError:
            tag_name = ""if tag_name == "p":
            print nextNode.stringelse:
            print"*****"break

Given:

<h2>section1</h2>
<p>article1</p>
<p>article2</p>
<p>article3</p>

<h2>section2</h2>
<p>article4</p>
<p>article5</p>
<p>article6</p>

Output:

article1
article2
article3
*****
article4
article5
article6
*****

Solution 2:

The next_siblings iterator can be helpful here as well:

for i in soup.find_all('h2'):
    for sib in i.next_siblings:
        if sib.name == 'p':
            print(sib.text)
        elif sib.name == 'h2':
            print ("*****")
            break

Post a Comment for "Find Next Siblings Until A Certain One Using Beautifulsoup"