Welcome to Foundation of Data Science Laboratory
Welcome to Foundation of Data Science Laboratory
Program 8: Navigating HTML with Parent and Sibling Relationships
Step 1: Install Required Libraries
pip install requests beautifulsoup4 pandas
from bs4 import BeautifulSoup
html = """
<html>
<body>
<h1>Heading</h1>
<p>Paragraph</p>
<div>Some <b>bold</b> text</div>
</body>
</html>
"""
soup = BeautifulSoup(html, 'html.parser')
# Navigate up to the parent of <b>
bold_tag = soup.b
parent_div = bold_tag.parent
print(parent_div.text) # Output: Some bold text
# Get the next sibling of the <h1> tag
h1 = soup.h1
next_sibling = h1.find_next_sibling()
print(next_sibling.text) # Output: Paragraph
Some bold text
Paragraph