Piloterr

Extract data from the HTML

Parse the title and headings out of the HTML.

Parse the HTML to extract specific elements. Here we extract the page title and all <h1> headings. Continue from Crawl a webpage:

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, "html.parser")

title = soup.find("title")
print("Page title:", title.text if title else "N/A")

for h in soup.find_all("h1"):
    print("H1:", h.get_text(" ", strip=True))