How to select values between two nodes in BeautifulSoup and Python?

You can select elements between two nodes in BeautifulSoup by looping through the main nodes, and checking the next siblings to see if a main node was reached: 

from bs4 import BeautifulSoup

html_content = '''

Element 1

Element 2

Element 3

''' soup = BeautifulSoup(html_content, 'html.parser') elements = [] for tag in soup.find("h1").next_siblings: if tag.name == "h1": break else: elements.append(tag) print(elements) # Output: [

Element 1

,

Element 2

,

Element 3

]

Related BeautifulSoup web scraping questions:

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *