"""乐天店铺页 __INITIAL_STATE__ → ShopDetailData 店铺首页(www.rakuten.co.jp/{店铺代码}/)与搜索页一样把状态内联在 `window.__INITIAL_STATE__` 里,店铺信息在顶层的 `shop` 节点下,结构比商品 详情页里的 shop.information 更完整(多出招牌图、39ショップ标记、休息日)。 店铺 logo 不在 state 里,只出现在页面的 ld+json AggregateRating 微数据中。 """ from __future__ import annotations import json import re from typing import Any from app.core import site from app.core.errors import ScrapeParseError from app.models.scrape import ShopDetailData from app.utils.coerce import as_dict, as_float, as_int, as_list, as_str _LD_JSON_RE = re.compile( r']*type="application/ld\+json"[^>]*>(.*?)', re.S ) def _parse_logo_url(html: str) -> str: """从 ld+json 的 AggregateRating.itemReviewed 里取店铺 logo""" for match in _LD_JSON_RE.finditer(html): try: data = json.loads(match.group(1)) except ValueError: continue if not isinstance(data, dict) or data.get("@type") != "AggregateRating": continue reviewed = data.get("itemReviewed") if isinstance(reviewed, dict): return as_str(reviewed.get("logo")) return "" def parse_shop_detail( state: dict[str, Any], *, shop_code: str, html: str = "" ) -> ShopDetailData: """把店铺页状态解析为商家详情 Raises: ScrapeParseError: 状态中不存在 shop 节点 """ shop = state.get("shop") if not isinstance(shop, dict) or not shop: raise ScrapeParseError("店铺页缺少 shop 节点") review = as_dict(shop.get("shopReview")) resolved_code = as_str(shop.get("shopCode")) or shop_code # state 里的 shopUrl 是 http 的且不带尾斜杠,统一按规范形式给出 shop_url = f"{site.WWW_BASE_URL}{resolved_code}/" if resolved_code else "" holidays = [ as_str(day) for day in as_list(as_dict(as_dict(shop.get("shopCalendar")).get("eventDates")).get("holiday")) if as_str(day) ] return ShopDetailData( shop_id=as_int(shop.get("shopId")) or None, shop_code=resolved_code, shop_name=as_str(shop.get("shopName")), shop_url=shop_url, introduction=as_str(shop.get("shopIntroduction")), signboard_url=as_str(shop.get("signboardUrl")), logo_url=_parse_logo_url(html), review_score=as_float(review.get("average")), review_count=as_int(review.get("total")), # 评价数过少时站点不展示评分,此时 average 不可信 review_displayed=bool(review.get("didMeetDisplayRequirements")), is_39_shop=bool(shop.get("is39Shop")), age_verification_required=bool(shop.get("ageVerificationFlag")), status=as_int(shop.get("status")) if shop.get("status") is not None else None, holidays=holidays, )