Skip to content

FINDING MULTIPLE TAGS - Searching for different tags

Python
#!/usr/bin/env python3
"""FINDING MULTIPLE TAGS - Searching for different tags"""
from bs4 import BeautifulSoup
html = """
<html><body>
<h1>Title</h1>
<h2>Subtitle</h2>
<p>Paragraph</p>
<div>Division</div>
<span>Span</span>
</body></html>
"""
soup = BeautifulSoup(html, 'html.parser')
print("Finding Multiple Tags:")
headings = soup.find_all(['h1', 'h2', 'h3'])
print(f"  Headings: {[h.string for h in headings]}")
blocks = soup.find_all(['p', 'div'])
print(f"  Blocks: {[b.string for b in blocks]}")
all_text = soup.find_all(['h1', 'h2', 'p', 'div', 'span'])
print(f"  All text elements: {len(all_text)}")