branch_number检测逻辑
This commit is contained in:
@@ -376,7 +376,13 @@ class SurugayaClient:
|
||||
shipping_note_node = row.css_first("ul.shipping_campaing_info li.padT5")
|
||||
shipping_note = shipping_note_node.text().strip() if shipping_note_node else ""
|
||||
|
||||
# branch_number 优先从 detail_url 的查询参数中提取(与 detail_url 保持一致),
|
||||
# data-zaiko_data 仅作为兜底(极少数情况下 href 未携带 branch_number 时)。
|
||||
branch_number = ""
|
||||
if detail_href:
|
||||
branch_number = (parse_qs(urlparse(detail_href).query).get("branch_number") or [""])[0]
|
||||
|
||||
if not branch_number:
|
||||
campaign_node = row.css_first("li.ajax-campaign-placeholder")
|
||||
if campaign_node:
|
||||
zaiko_raw = campaign_node.attributes.get("data-zaiko_data") or ""
|
||||
|
||||
@@ -74,17 +74,35 @@ OTHER_SHOP_HTML = """
|
||||
</ul>
|
||||
</td>
|
||||
</tr>
|
||||
<tr class="item">
|
||||
<td><p><strong class="text-red text-bold mgnL10">980円</strong></p></td>
|
||||
<td>
|
||||
<a href="/product/detail/602274031?tenpo_cd=400567&branch_number=0003">
|
||||
<h2 class="title_product">中古</h2>
|
||||
</a>
|
||||
</td>
|
||||
<td>
|
||||
<div class="space_text_1 mgnB5"><strong><a href="/shop/400567">駿河屋 テスト店</a></strong></div>
|
||||
<div class="star_group mgnB5"></div>
|
||||
<div>4.5(10件)</div>
|
||||
</td>
|
||||
<td>
|
||||
<ul class="shipping_campaing_info">
|
||||
<li class="padT5">1日〜3日以内に発送します</li>
|
||||
</ul>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
"""
|
||||
|
||||
|
||||
def test_parse_other_shop_list_extracts_all_rows() -> None:
|
||||
"""三行分别为:加盟店、官方自营、市场店铺,均应被正确解析。"""
|
||||
"""四行分别为:加盟店、官方自营、市场店铺、无 data-zaiko_data 的加盟店,均应被正确解析。"""
|
||||
tree = HTMLParser(OTHER_SHOP_HTML)
|
||||
items = SurugayaClient._parse_other_shop_list(tree)
|
||||
|
||||
assert len(items) == 3
|
||||
assert len(items) == 4
|
||||
|
||||
|
||||
def test_parse_other_shop_list_franchise_row() -> None:
|
||||
@@ -133,6 +151,17 @@ def test_parse_other_shop_list_marketplace_row() -> None:
|
||||
assert item.rating_count == 46
|
||||
|
||||
|
||||
def test_parse_other_shop_list_branch_number_falls_back_to_detail_url() -> None:
|
||||
"""无 ajax-campaign-placeholder(无 data-zaiko_data)时,branch_number 应从 detail_url 查询参数回退提取。"""
|
||||
tree = HTMLParser(OTHER_SHOP_HTML)
|
||||
item = SurugayaClient._parse_other_shop_list(tree)[3]
|
||||
|
||||
assert item.branch_number == "0003"
|
||||
assert item.detail_url == (
|
||||
"https://www.suruga-ya.jp/product/detail/602274031?tenpo_cd=400567&branch_number=0003"
|
||||
)
|
||||
|
||||
|
||||
def test_parse_other_shop_list_empty_html_returns_empty_list() -> None:
|
||||
"""页面结构不包含 tbl_all 时(如解析失败/改版),应返回空列表而非抛异常。"""
|
||||
tree = HTMLParser("<html><body>no table here</body></html>")
|
||||
|
||||
Reference in New Issue
Block a user