fix(tradein): Avito-парсер тащит фото и реальный адрес из DOM #419
1 changed files with 23 additions and 5 deletions
|
|
@ -295,7 +295,8 @@ class AvitoScraper(BaseScraper):
|
|||
return None
|
||||
|
||||
def _dom_card_to_lot(self, card: Any, source_url_base: str) -> ScrapedLot | None:
|
||||
"""Парсинг через DOM (fallback). Avito state часто пустой — items падают сюда."""
|
||||
"""Парсинг через DOM. Avito state давно пустой (см. _parse_html) — почти все
|
||||
объявления приходят сюда, поэтому путь обязан тащить и адрес, и фото."""
|
||||
try:
|
||||
link_el = card.css_first('a[data-marker="item-title"]')
|
||||
if link_el is None:
|
||||
|
|
@ -313,8 +314,24 @@ class AvitoScraper(BaseScraper):
|
|||
area = _extract_area_from_title(title)
|
||||
floor, total = _extract_floor_from_title(title)
|
||||
|
||||
addr_el = card.css_first('[class*="geo-address"]')
|
||||
address = addr_el.text(strip=True) if addr_el is not None else None
|
||||
# Адрес: первый <p> внутри data-marker="item-location" — это улица + дом.
|
||||
# Второй <p> — метро/район, его отбрасываем. Старый селектор
|
||||
# [class*="geo-address"] мёртв: Avito перешёл на CSS-модули с хешами.
|
||||
address: str | None = None
|
||||
loc_el = card.css_first('[data-marker="item-location"]')
|
||||
if loc_el is not None:
|
||||
street_el = loc_el.css_first("p")
|
||||
if street_el is not None:
|
||||
address = street_el.text(strip=True) or None
|
||||
|
||||
# Фото: <img itemprop="image"> карточки-слайдера, реальный URL прямо в src.
|
||||
photo_urls: list[str] = []
|
||||
for img in card.css('img[itemprop="image"]'):
|
||||
src = img.attributes.get("src") or ""
|
||||
if src.startswith("http") and src not in photo_urls:
|
||||
photo_urls.append(src)
|
||||
if len(photo_urls) >= 5:
|
||||
break
|
||||
|
||||
# Anchor jitter — Avito DOM не отдаёт точные coords, используем центр scrape ±jitter
|
||||
lat = lon = None
|
||||
|
|
@ -335,9 +352,10 @@ class AvitoScraper(BaseScraper):
|
|||
floor=floor,
|
||||
total_floors=total,
|
||||
price_rub=price,
|
||||
raw_payload={"title": title},
|
||||
photo_urls=photo_urls,
|
||||
raw_payload={"title": title, "address": address},
|
||||
)
|
||||
except Exception: # noqa: BLE001
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue