0 dní v řadě · 0 bodů

Tahák · Python · Užitečné knihovny

BeautifulSoup: čtení webových stránek

Z HTML vytáhneš nadpisy, odkazy, ceny i celé tabulky a napíšeš scraper, který nespadne při první změně webu.

Otevřít celou lekciVšechny taháky
python
from bs4 import BeautifulSoup              # pip install beautifulsoup4

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

soup.find("h1")                            # first match or None
soup.find_all("a", class_="x")             # list of matches
soup.select("div.karta > a[href]")         # CSS selector, list
soup.select_one("#menu")                   # CSS selector, first or None

tag.get_text(" ", strip=True)              # clean text
tag["href"]                                # attribute, KeyError if missing
tag.get("href")                            # attribute or None
tag.find_parent("article")                 # go up
tag.find_next_sibling("dd")                # go sideways

urllib.parse.urljoin(page_url, href)       # absolute link