import re
import pandas as pd
import requests
from bs4 import BeautifulSoup
URL = "https://www.eunasa.com/es/articulo/despiece_eunasa/lavavajillas/aristarco/679_lavado_aclarado"
CSV_EQ = "equivalencias_sku_eunasa.csv" # tu fichero
# 1) Cargar equivalencias eunasa_ref -> sku
eq = pd.read_csv(CSV_EQ, dtype=str).fillna("")
eq["eunasa_ref"] = eq["eunasa_ref"].str.strip()
eq["product_sku"] = eq["product_sku"].str.strip()
map_e2sku = dict(zip(eq["eunasa_ref"], eq["product_sku"]))
def eunasa_to_sku(ref: str) -> str:
ref = str(ref).strip()
if ref in map_e2sku:
return map_e2sku[ref]
z6 = ref.zfill(6)
if z6 in map_e2sku:
return map_e2sku[z6]
return "" # no match
html = requests.get(URL, timeout=30).text
soup = BeautifulSoup(html, "html.parser")
# 2) Extraer bloques de producto
# Nota: esto depende de la estructura; aquí hago un enfoque robusto por patrón "Ref:"
text = soup.get_text("\n")
# Localiza líneas tipo: "Nombre ... Ref: 116"
# (En la página se ve ese patrón)
items = []
lines = [ln.strip() for ln in text.splitlines() if ln.strip()]
for i, ln in enumerate(lines):
m = re.search(r"\bRef:\s*(\d{2,6})\b", ln)
if m:
ref = m.group(1)
# el nombre suele estar 1-2 líneas arriba; buscamos hacia atrás el primer “título” razonable
name = ""
for back in range(1, 6):
if i-back >= 0 and len(lines[i-back]) > 6 and "Ref:" not in lines[i-back] and "Cantidad" not in lines[i-back]:
name = lines[i-back]
break
sku = eunasa_to_sku(ref) or "CONSULTAR"
items.append((sku, ref, name))
# 3) Generar HTML
rows = "\n".join(
f'
| {sku} | {ref} | {name} |
'
for sku, ref, name in items
)
out = f"""
Aristarco · ALISEO 422-428 · Lavado-Aclarado
| Mi ref. |
Ref. despiece |
Producto |
{rows}
"""
with open("despiece_transformado.html", "w", encoding="utf-8") as f:
f.write(out)
print("OK -> despiece_transformado.html")