42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
from bs4 import BeautifulSoup
|
|
|
|
# 1) jeśli masz body jako string:
|
|
# html = "<body>....</body>"
|
|
|
|
# 2) albo wczytaj z pliku lokalnego:
|
|
with open("guitar2.html", "r", encoding="utf-8") as f:
|
|
html = f.read()
|
|
|
|
soup = BeautifulSoup(html, "html.parser")
|
|
|
|
# model — najpierw input[name="s_nr_kat"]
|
|
model = None
|
|
inp = soup.find("input", {"name": "s_nr_kat"})
|
|
if inp and inp.has_attr("value"):
|
|
model = inp["value"].strip()
|
|
|
|
# jeśli trzeba, fallback: znajdź <b>Model</b> i pobierz input w następnym <td>
|
|
if not model:
|
|
b = next((b for b in soup.find_all("b") if b.get_text(strip=True).lower() == "model"), None)
|
|
if b:
|
|
td = b.find_parent("td")
|
|
if td:
|
|
next_td = td.find_next_sibling("td")
|
|
if next_td:
|
|
inp2 = next_td.find("input")
|
|
if inp2 and inp2.has_attr("value"):
|
|
model = inp2["value"].strip()
|
|
|
|
# nr zamówienia — span.czarnobiale (może zawierać <b>)
|
|
order = None
|
|
span = soup.find("span", class_="czarnobiale")
|
|
if span:
|
|
order = span.get_text(strip=True)
|
|
|
|
# dodatkowe oczyszczanie (usuwa ewentualne spacje)
|
|
if order:
|
|
order = order.replace("\n", " ").strip()
|
|
|
|
print("Model:", model)
|
|
print("Nr zam.:", order)
|