Files
rakuten-api/app/scraping/parsers/subsites/biccamera.py
T
q792602257andClaude Opus 5 104d7fef6b 拆分抓取与交易服务
把需要账号登录态的链路从抓取服务里拆出成独立进程。分界线不是「要不要登录」,
而是抓取无状态、幂等、可多开实例,而交易的写操作不可逆、登录态全局唯一、
订单监控是常驻轮询——同进程时抓取一扩容就会复制出 N 份登录态与 N 个轮询,
同一账号会被并发操作。

- app/shared:配置、错误码、日志、ApiResponse 信封 + Bearer 鉴权 + 异常处理器、
  导航请求头构造器
- app/scraping:站点常量、会话、解析器与 10 个抓取接口,:31107,可多开
- app/trading:登录态查询/重载与健康检查,:31108,只能单实例
- 依赖方向锁为 scraping→shared、trading→shared,两侧互不 import;
  tests/test_architecture.py 用 AST 检查 import 并校验两个 app 的路径不串
- 登录态 UA 在 trading 独立持有:与抓取 UA 值相同但变更理由不同,抓取 UA 为绕
  反爬可随时调整,登录 UA 一改可能触发设备校验使已落盘 cookie 失效
- scripts/login.py 与 AuthSession 共用 auth_site.PROFILES 与 is_logged_in,判据只写一遍
- 同一镜像两个启动命令,交易容器覆盖 command 并设 RAKUTEN_HEALTH_PORT

同时带上此前未提交的 ラクマ 分类接口与登录态基础设施。

验证:239 个离线用例全绿;两个入口真实启动,/health 与鉴权正常。
未验证:真实探测登录态(当前开发机无外网,对站点的连接全部超时)。

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-27 15:05:01 +08:00

133 lines
4.9 KiB
Python

"""ビックカメラ楽天市場店(biccamera.rakuten.co.jp)商品页解析
Nuxt 应用,整页数据内联在 `window.__NUXT__`(纯 JSON 对象,可直接增量解析)。
商品主体在 `state.item`,字段命名已经很接近市场侧语义,且直接给出市场的
shop_id / genre_id / 分类路径与库存数。
站点不提供:SKU 组合(该店商品都是单一规格)、商品评分(异步加载)。
"""
from __future__ import annotations
import json
import re
from app.shared.errors import ScrapeParseError
from app.scraping.models.scrape import (
Breadcrumb,
ItemDetailData,
PurchaseInfo,
ShippingInfo,
ShopSummary,
SkuInfo,
)
from app.scraping.parsers.subsites.base import SubsitePage
from app.scraping.utils.coerce import as_dict, as_int, as_list, as_str
HOST = "biccamera.rakuten.co.jp"
SOURCE = "biccamera"
SHOP_NAME = "ビックカメラ楽天市場店"
_NUXT_RE = re.compile(r"window\.__NUXT__\s*=\s*")
_CATEGORY_URL = "https://www.rakuten.co.jp/category/{}/"
def validate(html: str) -> str | None:
if _NUXT_RE.search(html):
return None
return f"biccamera nuxt state not found (body {len(html)} bytes)"
def _extract_nuxt(html: str) -> dict:
match = _NUXT_RE.search(html)
if match is None:
raise ScrapeParseError("ビックカメラ页面未找到 window.__NUXT__")
try:
state, _ = json.JSONDecoder().raw_decode(html, match.end())
except ValueError as exc:
raise ScrapeParseError(f"window.__NUXT__ 解析失败:{exc}") from exc
if not isinstance(state, dict):
raise ScrapeParseError("window.__NUXT__ 不是 JSON 对象")
return state
def _purchase_info(state: dict, item: dict) -> PurchaseInfo:
"""该站加购走自己的 JSON 接口,字段与市场完全不同(没有 shop_bid)
选项(choices)的取值结构未取到样本验证,因此只如实回报「有没有选项」,
不给出可能不准确的选项定义——带选项的商品需要调用方另行处理。
"""
choices = as_list(state.get("choices"))
return PurchaseInfo(
cart_url=as_str(item.get("add_cart_api_url")),
form_fields={"item_id": str(as_int(item.get("item_id")))},
quantity_field="units",
options_field="choice" if choices else "",
has_required_options=bool(choices),
)
def parse(page: SubsitePage) -> ItemDetailData:
"""解析ビックカメラ商品页"""
nuxt = _extract_nuxt(page.html)
item = as_dict(as_dict(nuxt.get("state")).get("item"))
if not item or not as_str(item.get("item_name")):
raise ScrapeParseError("ビックカメラ页面缺少 state.item")
sold_out = bool(item.get("sold_out_flag"))
inventory = as_int(item.get("inventory"))
delivery = as_str(item.get("delivery_schedule_text"))
breadcrumbs = [
Breadcrumb(
name=as_str(genre.get("genre_name")),
url=_CATEGORY_URL.format(as_str(genre.get("genre_id"))),
)
for genre in as_list(item.get("genres"))
if isinstance(genre, dict) and as_str(genre.get("genre_name"))
]
images = [
as_str(image.get("url"))
for image in as_list(item.get("images"))
if isinstance(image, dict) and as_str(image.get("url"))
]
shop_code = as_str(item.get("shop_url")) or page.shop_code
return ItemDetailData(
source=SOURCE,
source_url=page.final_url,
item_id=str(as_int(item.get("item_id"))) if item.get("item_id") is not None else "",
item_code=as_str(item.get("item_number")) or page.item_code,
item_name=as_str(item.get("item_name")),
catch_copy=as_str(item.get("catch_copy")),
description=as_str(item.get("caption")),
item_url=page.requested_url,
price=as_int(item.get("price_with_tax")),
pre_tax_price=as_int(item.get("original_price")),
tax_flag=bool(item.get("included_tax_flag")),
purchase_condition="soldOut" if sold_out else "enabled",
is_sold_out=sold_out,
purchase_unit=as_int(item.get("units")),
images=images,
shop=ShopSummary(
shop_id=as_int(item.get("shop_id")) or None,
shop_code=shop_code,
shop_name=SHOP_NAME,
shop_url=f"https://www.rakuten.co.jp/{shop_code}/",
),
genre_id=str(as_int(item.get("genre_id"))) if item.get("genre_id") is not None else "",
breadcrumbs=breadcrumbs,
shipping=ShippingInfo(
# 该店商品价格含运费时站点会置位此标记,不再单独给运费金额
is_shipping_free=bool(item.get("included_shipping_fee_flag")),
delivery_message=delivery,
),
sku=SkuInfo(
inventory_type="single",
quantity=inventory,
show_inventory=inventory > 0,
delivery_message=delivery,
),
purchase=_purchase_info(as_dict(nuxt.get("state")), item),
)