|
| 1 | +""" |
| 2 | +One-shot diagnostic for ``ebay_sold_scrape_service``. |
| 3 | +
|
| 4 | +Fetches the same URL the production service uses, dumps the HTML to |
| 5 | +``/tmp/ebay-sold.html``, and reports how many elements match candidate |
| 6 | +selectors so we can pick the right one when eBay rotates its SRP layout. |
| 7 | +
|
| 8 | +Run from the ``api/`` directory: |
| 9 | +
|
| 10 | + python -m scripts.debug_ebay_scrape "carte pokemon" |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import asyncio |
| 16 | +import sys |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +from bs4 import BeautifulSoup |
| 20 | + |
| 21 | +from services.ebay_sold_scrape_service import ( |
| 22 | + _parse_sold_rows, |
| 23 | + fetch_sold_listings_html, |
| 24 | +) |
| 25 | + |
| 26 | +OUT = Path("/tmp/ebay-sold.html") |
| 27 | + |
| 28 | +#: Candidate selectors to probe. Order is informational; we report counts for all. |
| 29 | +_CANDIDATE_SELECTORS = ( |
| 30 | + "li.s-item", |
| 31 | + "ul.srp-results > li", |
| 32 | + ".srp-results .s-item", |
| 33 | + ".srp-results .s-item__wrapper", |
| 34 | + "li.s-item__pl-on-bottom", |
| 35 | + "[data-testid='srp-results'] li", |
| 36 | + "[data-view*='mi:1686'] li", |
| 37 | + "ul.b-list__items_nofooter li", |
| 38 | + "li[data-viewport]", |
| 39 | + "div.s-card", |
| 40 | +) |
| 41 | + |
| 42 | +#: Selectors that often hold the relative/absolute « sold » caption. |
| 43 | +_CAPTION_SELECTORS = ( |
| 44 | + ".s-item__caption--signal", |
| 45 | + ".s-item__title--tagblock", |
| 46 | + ".s-item__subtitle", |
| 47 | + ".s-card__caption", |
| 48 | + "[class*='caption']", |
| 49 | +) |
| 50 | + |
| 51 | + |
| 52 | +async def main(q: str) -> None: |
| 53 | + html = await fetch_sold_listings_html(q=q, page_size=50) |
| 54 | + OUT.write_text(html, encoding="utf-8") |
| 55 | + print(f"saved html ({len(html)} bytes) → {OUT}") |
| 56 | + |
| 57 | + soup = BeautifulSoup(html, "html.parser") |
| 58 | + |
| 59 | + title = soup.select_one("title") |
| 60 | + print(f"<title>: {title.get_text(strip=True) if title else '(none)'}") |
| 61 | + |
| 62 | + h1 = soup.select_one("h1") |
| 63 | + print(f"<h1>: {h1.get_text(' ', strip=True)[:120] if h1 else '(none)'}") |
| 64 | + |
| 65 | + # Quick consent-page heuristic |
| 66 | + consent_markers = ("consent", "consentement", "accepter", "vos choix") |
| 67 | + head_excerpt = html[:4000].lower() |
| 68 | + if any(tok in head_excerpt for tok in consent_markers): |
| 69 | + print("⚠️ consent-related token found in first 4 KB — possible CMP page") |
| 70 | + |
| 71 | + print("\n-- selector probe --") |
| 72 | + for sel in _CANDIDATE_SELECTORS: |
| 73 | + try: |
| 74 | + n = len(soup.select(sel)) |
| 75 | + except Exception as exc: # invalid selector etc. |
| 76 | + n = f"ERR({exc})" |
| 77 | + print(f" {sel:55s} → {n}") |
| 78 | + |
| 79 | + print("\n-- existing parser --") |
| 80 | + rows = _parse_sold_rows(html) |
| 81 | + print(f" _parse_sold_rows: {len(rows)} rows") |
| 82 | + for r in rows[:3]: |
| 83 | + print(f" title={r.title[:60]!r} caption={r.sold_caption!r} hours_ago={r.approx_hours_ago}") |
| 84 | + |
| 85 | + # If selector probe found something useful, sample captions |
| 86 | + print("\n-- sample captions from first li.s-item or fallback --") |
| 87 | + sample_lis = soup.select("li.s-item") or soup.select("li.s-item__pl-on-bottom") or soup.select("ul.srp-results > li") |
| 88 | + for i, li in enumerate(sample_lis[:5]): |
| 89 | + for csel in _CAPTION_SELECTORS: |
| 90 | + cap = li.select_one(csel) |
| 91 | + if cap: |
| 92 | + print(f" li#{i} via {csel}: {cap.get_text(' ', strip=True)[:120]!r}") |
| 93 | + break |
| 94 | + else: |
| 95 | + print(f" li#{i} (no caption matched any selector)") |
| 96 | + |
| 97 | + # Field probes on the first 2 LIs so we can pin down the new s-card selectors |
| 98 | + field_probes: dict[str, tuple[str, ...]] = { |
| 99 | + "title": ( |
| 100 | + ".s-card__title", ".s-card__title-link", |
| 101 | + "[role='heading']", "[role=heading]", |
| 102 | + "a .su-styled-text", ".s-item__title", ".s-item__title span", |
| 103 | + ), |
| 104 | + "price": (".s-card__price", ".s-item__price", "[class*='price']"), |
| 105 | + "link": ("a.su-link", "a[href*='/itm/']", "a.s-item__link"), |
| 106 | + "image": ( |
| 107 | + ".s-card__image img", ".s-card__image-wrapper img", |
| 108 | + "img.s-item__image-img", ".image-treatment img", "img", |
| 109 | + ), |
| 110 | + "caption": _CAPTION_SELECTORS, |
| 111 | + } |
| 112 | + |
| 113 | + print("\n-- field selector probe (first 2 LIs) --") |
| 114 | + for i, li in enumerate(sample_lis[:2]): |
| 115 | + print(f"\n[li #{i}]") |
| 116 | + for field, sels in field_probes.items(): |
| 117 | + for s in sels: |
| 118 | + el = li.select_one(s) |
| 119 | + if not el: |
| 120 | + continue |
| 121 | + if field == "link": |
| 122 | + snippet = (el.get("href") or "")[:120] |
| 123 | + elif field == "image": |
| 124 | + snippet = (el.get("src") or el.get("data-src") or "")[:120] |
| 125 | + else: |
| 126 | + snippet = el.get_text(" ", strip=True)[:120] |
| 127 | + print(f" {field:7s} via {s:35s} → {snippet!r}") |
| 128 | + break |
| 129 | + else: |
| 130 | + print(f" {field:7s} no match") |
| 131 | + # Also dump the LI's outer HTML head (200 chars) so we can see attributes |
| 132 | + outer = str(li)[:300].replace("\n", " ") |
| 133 | + print(f" outer[:300]: {outer}") |
| 134 | + |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + query = sys.argv[1] if len(sys.argv) > 1 else "carte pokemon" |
| 138 | + asyncio.run(main(query)) |
0 commit comments