mirror of
https://github.com/R0m1k3/Priceflow.git
synced 2026-10-24 07:38:13 +02:00
feat: Add abstract base parser with product result dataclass and implement Stokomani parser for search results.
This commit is contained in:
1 parent
ebe685fdf9
commit
078060f155
9 files changed
+136
-137
No files matched your search
@@ -13,32 +13,59 @@ class CentrakorParser(BaseParser):
|
||||
results = []
|
||||
|
||||
# Centrakor products
|
||||
# Config: a.link.link--block, a.product-item__link
|
||||
# Config: div.product-item, div.product-card
|
||||
|
||||
products = soup.select(".product-item, .product-card")
|
||||
# Try finding cards first
|
||||
cards = soup.select("div.product-item, div.product-card, article")
|
||||
|
||||
for product in products:
|
||||
# If no cards found, try finding by link and getting parent
|
||||
if not cards:
|
||||
links = soup.select("a[href*='/p/'], a[href*='/produit/'], a.product-item__link")
|
||||
cards = []
|
||||
for link in links:
|
||||
# Try to find a container div
|
||||
parent = link.find_parent("div", class_=lambda x: x and ("product" in x or "item" in x))
|
||||
if parent:
|
||||
cards.append(parent)
|
||||
else:
|
||||
cards.append(link.parent) # Fallback to immediate parent
|
||||
|
||||
for card in cards:
|
||||
try:
|
||||
link_el = product.select_one("a.product-item__link, a.link")
|
||||
link_el = card.select_one("a.product-item__link, a.link, a[href*='/p/']")
|
||||
if not link_el:
|
||||
continue
|
||||
# If card is the link itself
|
||||
if card.name == 'a' and card.get('href'):
|
||||
link_el = card
|
||||
else:
|
||||
continue
|
||||
|
||||
href = link_el.get('href')
|
||||
url = self.make_absolute_url(href)
|
||||
|
||||
title_el = product.select_one(".product-item__name, .product-card__title")
|
||||
title = title_el.get_text(strip=True) if title_el else None
|
||||
title_el = card.select_one(".product-item__name, .product-card__title, h3, h2")
|
||||
title = title_el.get_text(strip=True) if title_el else link_el.get_text(strip=True)
|
||||
|
||||
if not title:
|
||||
continue
|
||||
|
||||
img_url = self.extract_image_url(product)
|
||||
img_url = None
|
||||
img_el = card.select_one("img")
|
||||
if img_el:
|
||||
img_url = self._get_image_src(img_el)
|
||||
if img_url:
|
||||
img_url = self.make_absolute_url(img_url)
|
||||
|
||||
price = None
|
||||
price_el = product.select_one(".price, .product-price")
|
||||
price_el = card.select_one(".price, .product-price, .product-item__price")
|
||||
if price_el:
|
||||
price = self.parse_price_text(price_el.get_text())
|
||||
|
||||
# Fallback price search in text
|
||||
if not price:
|
||||
text = card.get_text(" ", strip=True)
|
||||
price = self.parse_price_text(text)
|
||||
|
||||
results.append(ProductResult(
|
||||
title=title,
|
||||
url=url,
|
||||
|
||||
Reference in new issue
Block a user