feat: Add Stokomani search results parser

This commit is contained in:
Michael committed 2025-11-30 18:13:11 +01:00
1 parent c2f042beee
commit b3d5c56707
1 file changed
+23 -40
+23 -40
View File
@@ -9,13 +9,12 @@ class StokomaniParser(BaseParser):
super().__init__("stokomani.fr", "https://www.stokomani.fr") super().__init__("stokomani.fr", "https://www.stokomani.fr")
def parse_search_results(self, html: str, query: str, search_url: str) -> list[ProductResult]: def parse_search_results(self, html: str, query: str, search_url: str) -> list[ProductResult]:
from bs4 import BeautifulSoup, NavigableString
soup = BeautifulSoup(html, "html.parser") soup = BeautifulSoup(html, "html.parser")
results = [] results = []
# Find product cards # Find product title links
# Based on inspection, links are a.reversed-link.block links = soup.select("a.reversed-link.block")
# We'll look for the container of these links
links = soup.select("a.reversed-link.block, a[href*='/products/']")
seen_urls = set() seen_urls = set()
@@ -25,54 +24,38 @@ class StokomaniParser(BaseParser):
if not href or href in seen_urls: if not href or href in seen_urls:
continue continue
# Filter out non-product links
if '/products/' not in href: if '/products/' not in href:
continue continue
seen_urls.add(href) seen_urls.add(href)
url = self.make_absolute_url(href) url = self.make_absolute_url(href)
# Title
title = link.get_text(strip=True) title = link.get_text(strip=True)
if not title:
# Try finding title in nested elements
title_el = link.find(class_=lambda x: x and 'title' in x)
if title_el:
title = title_el.get_text(strip=True)
if not title: if not title:
continue continue
# Find parent card to scope image and price search # Image: Look for the preceding <a> with aria-label
# Usually the card is a few levels up img_url = None
card = link.find_parent("div", class_=lambda x: x and ("product" in x or "card" in x or "item" in x)) prev_a = link.find_previous_sibling("a", attrs={"aria-label": True})
if not card: if prev_a and prev_a.get('href') == href:
# Fallback: use the link's parent img_url = self.extract_image_url(prev_a)
card = link.parent.parent
# Image # Price: Look for text node after the link
img_url = self.extract_image_url(card)
# Price
price = None price = None
# Try specific price selectors first next_sibling = link.next_sibling
price_selectors = [ while next_sibling:
".price", ".money", ".current-price", if isinstance(next_sibling, NavigableString):
"span[class*='price']", "div[class*='price']" price_text = next_sibling.strip()
] if "€" in price_text:
price = self.parse_price_text(price_text)
for selector in price_selectors: if price:
price_el = card.select_one(selector) break
if price_el: elif next_sibling.name == 'div' and 'price' in str(next_sibling.get('class', [])):
price = self.parse_price_text(price_el.get_text()) # Try finding price in next div if it's a price container
if price: price = self.parse_price_text(next_sibling.get_text())
break if price:
break
# Fallback: Look for price pattern in the card text next_sibling = next_sibling.next_sibling
if not price:
# Get text but exclude the title to avoid false positives if title has numbers
card_text = card.get_text(" ", strip=True)
price = self.parse_price_text(card_text)
results.append(ProductResult( results.append(ProductResult(
title=title, title=title,