Piloterr

Extrae datos del HTML

Extrae el título y los encabezados del HTML.

Analiza el HTML para extraer elementos concretos. Aquí extraemos el título de la página y todos los encabezados <h1>. Continúa a partir de Rastrea una página web:

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))