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

74 lines
2.5 KiB
Python

from bs4 import BeautifulSoup
from app.services.parsers.base_parser import BaseParser, ProductResult
import logging
logger = logging.getLogger(__name__)
class DartyParser(BaseParser):
def __init__(self):
super().__init__("darty.com", "https://www.darty.com")
def parse_search_results(self, html: str, query: str, search_url: str) -> list[ProductResult]:
soup = BeautifulSoup(html, "html.parser")
results = []
# Darty products
# Config: a[href*='/nav/achat/'][href*='.html']
cards = soup.select(".product-card, .product_item, div[class*='product-card']")
for card in cards:
try:
# Link
link_el = card.select_one("a[href*='/nav/achat/'], a.product-link")
if not link_el:
# Try finding any link if specific selector fails
link_el = card.find("a")
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(".product_name, .product-title, h2, h3")
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(".product_price, .price, [class*='price']")
if price_el:
price = self.parse_price_text(price_el.get_text())
results.append(ProductResult(
title=title,
url=url,
source="Darty",
price=price,
currency="EUR",
in_stock=True,
image_url=img_url,
snippet=f"Product from Darty"
))
except Exception as e:
logger.error(f"Error parsing Darty product: {e}")
continue
logger.info(f"DartyParser found {len(results)} results")
return results