80 lines
2.5 KiB
Python
Executable File
80 lines
2.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Test du parser Backmarket avec un MacBook Air M2."""
|
|
|
|
from pricewatch.app.scraping.pw_fetch import fetch_playwright
|
|
from pricewatch.app.stores.backmarket.store import BackmarketStore
|
|
import json
|
|
|
|
# URL d'un MacBook Air M2 reconditionné
|
|
url = "https://www.backmarket.fr/fr-fr/p/apple-macbook-air-133-pouces-m2-2022"
|
|
|
|
print("=" * 80)
|
|
print("TEST BACKMARKET - MACBOOK AIR M2")
|
|
print("=" * 80)
|
|
print(f"\nURL: {url}")
|
|
|
|
# Fetch avec Playwright (obligatoire pour Backmarket)
|
|
print("\n[1/3] Récupération de la page avec Playwright...")
|
|
result = fetch_playwright(url, headless=True, timeout_ms=60000)
|
|
|
|
if not result.success:
|
|
print(f"❌ ÉCHEC du fetch: {result.error}")
|
|
exit(1)
|
|
|
|
print(f"✓ Page récupérée: {len(result.html):,} caractères")
|
|
print(f" Durée: {result.duration_ms}ms")
|
|
|
|
# Sauvegarder le HTML
|
|
html_file = "scraped/backmarket_macbook_pw.html"
|
|
with open(html_file, "w", encoding="utf-8") as f:
|
|
f.write(result.html)
|
|
print(f"✓ HTML sauvegardé: {html_file}")
|
|
|
|
# Parser avec BackmarketStore
|
|
print("\n[2/3] Parsing avec BackmarketStore...")
|
|
store = BackmarketStore()
|
|
snapshot = store.parse(result.html, url)
|
|
|
|
# Afficher les résultats
|
|
print("\n[3/3] RÉSULTATS DU PARSING")
|
|
print("-" * 80)
|
|
print(f"Source: {snapshot.source}")
|
|
print(f"URL: {snapshot.url}")
|
|
print(f"Reference (SKU): {snapshot.reference}")
|
|
print(f"Title: {snapshot.title}")
|
|
print(f"Price: {snapshot.price} {snapshot.currency}")
|
|
print(f"Stock: {snapshot.stock_status}")
|
|
print(f"Images: {len(snapshot.images)} images")
|
|
if snapshot.images:
|
|
for i, img in enumerate(snapshot.images[:3]):
|
|
print(f" [{i+1}] {img[:80]}...")
|
|
|
|
print(f"\nCategory: {snapshot.category or 'Non extraite'}")
|
|
print(f"Specs: {len(snapshot.specs)} caractéristiques")
|
|
if snapshot.specs:
|
|
for key, value in list(snapshot.specs.items())[:5]:
|
|
print(f" - {key}: {value}")
|
|
|
|
print(f"\nDebug status: {snapshot.debug.status}")
|
|
print(f"Debug errors: {len(snapshot.debug.errors)}")
|
|
for err in snapshot.debug.errors:
|
|
print(f" - {err}")
|
|
|
|
print(f"Debug notes: {len(snapshot.debug.notes)}")
|
|
for note in snapshot.debug.notes:
|
|
print(f" - {note}")
|
|
|
|
print(f"\nIs complete: {snapshot.is_complete()}")
|
|
|
|
# Export JSON
|
|
json_file = "scraped/backmarket_macbook_detail.json"
|
|
json_data = snapshot.to_dict()
|
|
with open(json_file, "w", encoding="utf-8") as f:
|
|
json.dump(json_data, f, indent=2, ensure_ascii=False)
|
|
|
|
print(f"\n✓ JSON sauvegardé: {json_file}")
|
|
|
|
print("\n" + "=" * 80)
|
|
print("TEST TERMINÉ")
|
|
print("=" * 80)
|