Piloterr
Guias práticosRastrear qualquer site

Extrair dados do HTML

Extraia do HTML o título e os títulos de seção.

Interprete o HTML para extrair elementos específicos. Aqui extraímos o título da página e todos os títulos <h1>. Continue a partir de Rastrear uma 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))