feat: implement a new persistent browser-based search service for e-commerce scraping.

This commit is contained in:
Michael committed 2025-12-01 01:11:42 +01:00
1 parent 8bad965e29
commit 8303e767f7
1 file changed
+15 -15
+15 -15
View File
@@ -467,7 +467,7 @@ class ImprovedSearchService:
@staticmethod @staticmethod
async def _extract_price(page: Page) -> float | None: async def _extract_price(page: Page) -> float | None:
"""Extract price using Hybrid Strategy: JSON-LD -> CSS -> AI""" """Extract price using Hybrid Strategy: JSON-LD -> AI -> CSS"""
import re import re
# STRATEGY 1: JSON-LD (Most reliable) # STRATEGY 1: JSON-LD (Most reliable)
@@ -497,7 +497,20 @@ class ImprovedSearchService:
except Exception as e: except Exception as e:
logger.debug(f" JSON-LD extraction failed: {e}") logger.debug(f" JSON-LD extraction failed: {e}")
# STRATEGY 2: CSS Selectors (Standard) # STRATEGY 2: AI Extraction (Priority over CSS as requested)
try:
logger.info(" 🤖 Attempting AI extraction (Priority Strategy)...")
html_content = await page.content()
title = await page.title()
ai_price = await AIPriceExtractor.extract_price(html_content, title)
if ai_price:
logger.info(f" 🤖 AI found price: {ai_price}€")
return ai_price
except Exception as e:
logger.error(f" AI extraction failed: {e}")
# STRATEGY 3: CSS Selectors (Fallback)
# PRIORITY 1: Selectors for sale/promotional prices (highest priority) # PRIORITY 1: Selectors for sale/promotional prices (highest priority)
sale_price_selectors = [ sale_price_selectors = [
@@ -596,19 +609,6 @@ class ImprovedSearchService:
logger.debug(f"Selected lowest price from {len(all_prices)} candidates: {lowest_price}€") logger.debug(f"Selected lowest price from {len(all_prices)} candidates: {lowest_price}€")
return lowest_price return lowest_price
# STRATEGY 3: AI Fallback (Gemma 2 / OpenRouter)
try:
logger.info(" 🤖 CSS extraction failed, attempting AI extraction...")
html_content = await page.content()
title = await page.title()
ai_price = await AIPriceExtractor.extract_price(html_content, title)
if ai_price:
logger.info(f" 🤖 AI found price: {ai_price}€")
return ai_price
except Exception as e:
logger.error(f" AI fallback failed: {e}")
logger.debug("No price found") logger.debug("No price found")
return None return None