108 lines
4.3 KiB
Python
108 lines
4.3 KiB
Python
"""乐天市场(rakuten.co.jp)站点常量
|
|
|
|
集中维护站点入口 URL、浏览器指纹参数,以及搜索页 URL 的排序码 / 筛选码映射。
|
|
|
|
排序码(`s=`)与筛选码(`f=`)并非猜测所得,而是从搜索页前端 bundle
|
|
(r.r10s.jp/com/assets/app/pages/search/javascript/pc-*.bundle.js)中的
|
|
URL→UiQuestion 转换逻辑里提取的枚举,与站点行为一一对应。
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from typing import Final
|
|
|
|
# ---- 站点入口 ----
|
|
HOME_URL: Final = "https://www.rakuten.co.jp/"
|
|
SEARCH_BASE_URL: Final = "https://search.rakuten.co.jp/search/mall/"
|
|
ITEM_BASE_URL: Final = "https://item.rakuten.co.jp/"
|
|
|
|
CATEGORY_BASE_URL: Final = "https://www.rakuten.co.jp/category/"
|
|
|
|
# 店铺首页形如 https://www.rakuten.co.jp/edion/
|
|
WWW_BASE_URL: Final = "https://www.rakuten.co.jp/"
|
|
|
|
SEARCH_HOST: Final = "search.rakuten.co.jp"
|
|
ITEM_HOST: Final = "item.rakuten.co.jp"
|
|
WWW_HOST: Final = "www.rakuten.co.jp"
|
|
|
|
# 取顶层分类列表用的哨兵关键词。
|
|
# 站点的分类分面(genreTree)与查询内容无关,任何关键词都会返回同一套 39 个顶层
|
|
# 分类;刻意用一个搜不到东西的词,可以拿到 count 全为 null 的干净列表,避免把
|
|
# 「某个关键词下的命中数」误当成分类的商品总数返回。
|
|
# 注意不能用单字母(如 a),站点对过短的拉丁关键词直接返回 503。
|
|
GENRE_FACET_PROBE_KEYWORD: Final = "zzzqqqxyz123"
|
|
|
|
# 站点侧限制:搜索结果最多只能翻到 pagination.subset 条(实测 6750),
|
|
# 再往后翻页返回空列表。用于计算 has_more,避免上游无意义地深翻。
|
|
DEFAULT_SUBSET_LIMIT: Final = 6750
|
|
|
|
# ---- 浏览器指纹 ----
|
|
# 搜索页用 PC UA;商品详情页必须用手机 UA,否则返回的是各店铺自定义的
|
|
# EUC-JP 老模板(无 __INITIAL_STATE__,只有面包屑 JSON-LD,无法结构化解析)。
|
|
PC_USER_AGENT: Final = (
|
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
|
|
"(KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
|
|
)
|
|
SP_USER_AGENT: Final = (
|
|
"Mozilla/5.0 (iPhone; CPU iPhone OS 17_5 like Mac OS X) AppleWebKit/605.1.15 "
|
|
"(KHTML, like Gecko) Version/17.5 Mobile/15E148 Safari/604.1"
|
|
)
|
|
|
|
ACCEPT_LANGUAGE: Final = "ja,en-US;q=0.9,en;q=0.8"
|
|
|
|
# 乐天前置 Akamai Bot Manager。请求头不完整时不会直接封禁,而是把响应
|
|
# 拖到 ~11s(实测与响应体大小无关,22 字节的响应同样耗时 11s);补齐
|
|
# 下列头并复用 Akamai 下发的 cookie 后,稳定在 ~0.6-0.9s。
|
|
def default_headers(*, mobile: bool) -> dict[str, str]:
|
|
"""构造一套完整的浏览器导航请求头。"""
|
|
return {
|
|
"User-Agent": SP_USER_AGENT if mobile else PC_USER_AGENT,
|
|
"Accept": (
|
|
"text/html,application/xhtml+xml,application/xml;q=0.9,"
|
|
"image/avif,image/webp,image/apng,*/*;q=0.8,"
|
|
"application/signed-exchange;v=b3;q=0.7"
|
|
),
|
|
"Accept-Language": ACCEPT_LANGUAGE,
|
|
"sec-ch-ua": '"Chromium";v="131", "Not_A Brand";v="24", "Google Chrome";v="131"',
|
|
"sec-ch-ua-mobile": "?1" if mobile else "?0",
|
|
"sec-ch-ua-platform": '"iOS"' if mobile else '"Windows"',
|
|
"Sec-Fetch-Dest": "document",
|
|
"Sec-Fetch-Mode": "navigate",
|
|
"Sec-Fetch-Site": "none",
|
|
"Sec-Fetch-User": "?1",
|
|
"Upgrade-Insecure-Requests": "1",
|
|
}
|
|
|
|
|
|
# Akamai Bot Manager 下发的 cookie:判断会话是否已预热完成的依据
|
|
AKAMAI_COOKIE_NAMES: Final = ("ak_bmsc", "bm_sv", "bm_mi", "_abck")
|
|
|
|
# ---- 排序(搜索页 `s=` 参数)----
|
|
# 键为对外暴露的语义化排序名,值为站点侧排序码;standard 不带 s 参数。
|
|
SORT_CODES: Final[dict[str, str | None]] = {
|
|
"standard": None, # 站点默认的相关度排序(relevancy)
|
|
"price_asc": "2",
|
|
"price_desc": "3",
|
|
"newest": "4",
|
|
"review_count": "5",
|
|
"review_score": "6",
|
|
"price_with_shipping_asc": "11",
|
|
"price_with_shipping_desc": "12",
|
|
}
|
|
|
|
# ---- 成色(搜索页 `f=` 参数中的 condition 段)----
|
|
CONDITION_CODES: Final[dict[str, str]] = {
|
|
"new": "101",
|
|
"used": "100",
|
|
"rental": "102",
|
|
}
|
|
|
|
# ---- 布尔筛选(搜索页 `f=` 参数,可重复出现)----
|
|
# 字段名 -> 筛选码
|
|
BOOL_FILTER_CODES: Final[dict[str, str]] = {
|
|
"include_sold_out": "0",
|
|
"free_shipping": "2",
|
|
"has_review": "4",
|
|
"next_day_delivery": "12",
|
|
"super_deal": "13",
|
|
}
|