Files
Priceflow/app/services/parsers/boulanger_parser.py
T

70 lines
2.4 KiB
Python

from bs4 import BeautifulSoup
from app.services.parsers.base_parser import BaseParser, ProductResult
import logging
logger = logging.getLogger(__name__)
class BoulangerParser(BaseParser):
def __init__(self):
super().__init__("boulanger.com", "https://www.boulanger.com")
def parse_search_results(self, html: str, query: str, search_url: str) -> list[ProductResult]:
soup = BeautifulSoup(html, "html.parser")
results = []
# Boulanger products
# Config: a[href*='/ref/'][href*='_']
cards = soup.select(".product-item, .product-list__item, article")
for card in cards:
try:
# Link
link_el = card.select_one("a[href*='/ref/'], a[class*='link']")
if not link_el:
continue
href = link_el.get('href')
if not href:
continue
url = self.make_absolute_url(href)
# Title
title = None
title_el = card.select_one("h2, h3, .product-label, [class*='title']")
if title_el:
title = title_el.get_text(strip=True)
else:
title = link_el.get_text(strip=True)
if not title:
continue
# Image
img_url = self.extract_image_url(card)
# Price
price = None
price_el = card.select_one(".price, .product-price, [class*='price']")
if price_el:
price = self.parse_price_text(price_el.get_text())
results.append(ProductResult(
title=title,
url=url,
source="Boulanger",
price=price,
currency="EUR",
in_stock=True,
image_url=img_url,
snippet=f"Product from Boulanger"
))
except Exception as e:
logger.error(f"Error parsing Boulanger product: {e}")
continue
logger.info(f"BoulangerParser found {len(results)} results")
return results