服装网(聚焦服饰、鞋包、配饰等时尚品类)的商品搜索功能(item_search接口,非官方命名)是获取服装类商品列表的核心入口。数据包含风格标签、尺码范围、材质成分、价格区间等时尚特有字段,对电商选品、潮流分析、比价工具等场景具有关键价值。由于平台无公开官方 API,开发者需通过页面解析实现搜索对接。本文系统讲解接口逻辑、参数解析、技术实现及时尚场景适配策略,助你构建稳定的服装商品列表获取系统。
一、接口基础认知(核心功能与场景)
二、对接前置准备(参数与 URL 结构)
三、接口调用流程(基于页面解析)
四、代码实现示例(Python)
import requests
import time
import random
import re
import urllib.parse
from bs4 import BeautifulSoup
from fake_useragent import UserAgent
from typing import List, Dict
class FuzhuangSearchApi:
def __init__(self, proxy_pool: List[str] = None, cookie: str = ""):
self.base_url = "https://www.fuzhuang.com/search/"
self.ua = UserAgent()
self.proxy_pool = proxy_pool # 代理池列表,如["http://ip:port", ...]
self.cookie = cookie # 登录态Cookie(用于完整价格和库存)
# 分类ID映射(简化版)
self.category_map = {
"女装-连衣裙": "101_208",
"男装-牛仔裤": "301_402",
"女鞋-马丁靴": "501_603"
}
def _get_headers(self) -> Dict[str, str]:
"""生成随机请求头"""
headers = {
"User-Agent": self.ua.random,
"Referer": "https://www.fuzhuang.com/category/",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
}
if self.cookie:
headers["Cookie"] = self.cookie
return headers
def _get_proxy(self) -> Dict[str, str]:
"""随机获取代理"""
if self.proxy_pool and len(self.proxy_pool) > 0:
proxy = random.choice(self.proxy_pool)
return {"http": proxy, "https": proxy}
return None
def _clean_price(self, price_str: str) -> float:
"""清洗价格字符串(去除¥、文字)"""
if not price_str:
return 0.0
price_str = re.sub(r"[^\d.]", "", price_str)
return float(price_str) if price_str else 0.0
def _clean_sales(self, sales_str: str) -> int:
"""清洗销量(提取数字,支持“1000+”)"""
if not sales_str:
return 0
sales_num = re.search(r"\d+", sales_str)
return int(sales_num.group()) if sales_num else 0
def _parse_item(self, item_soup) -> Dict[str, str]:
"""解析单条商品数据"""
# 提取商品ID
link = item_soup.select_one("a.product-title")["href"] if item_soup.select_one("a.product-title") else ""
item_id = re.search(r"/item/(\w+)\.html", link).group(1) if link else ""
# 提取风格标签
style_tags = [tag.text.strip() for tag in item_soup.select(".style-tags span")]
# 提取规格摘要(颜色+尺码)
spec_brief = item_soup.select_one(".spec-brief")?.text.strip() or ""
colors = []
sizes = []
if "颜色:" in spec_brief:
colors_part = re.split(r"颜色:", spec_brief)[1].split("|")[0].strip()
colors = [c.strip() for c in colors_part.split("/")]
if "尺码:" in spec_brief:
sizes_part = re.split(r"尺码:", spec_brief)[1].strip()
sizes = [s.strip() for s in sizes_part.split("/")]
return {
"item_id": item_id,
"title": item_soup.select_one(".product-title")?.text.strip() or "",
"main_image": item_soup.select_one(".product-img img")?.get("src") or "",
"url": f"https://www.fuzhuang.com{link}" if link.startswith("/") else link,
"price": {
"current": self._clean_price(item_soup.select_one(".price-current")?.text or ""),
"original": self._clean_price(item_soup.select_one(".price-original")?.text or "")
},
"style_tags": style_tags,
"specs": {
"colors": colors,
"sizes": sizes
},
"brand": item_soup.select_one(".brand-name")?.text.strip() or "",
"sales": {
"count": self._clean_sales(item_soup.select_one(".sales-count")?.text or ""),
"unit": "月销" if "月销" in (item_soup.select_one(".sales-count")?.text or "") else "总销"
},
"stock_status": item_soup.select_one(".stock-tag")?.text.strip() or "",
"rating": float(item_soup.select_one(".product-rating")?.text or "0")
}
def _parse_page(self, html: str) -> List[Dict]:
"""解析页面的商品列表"""
soup = BeautifulSoup(html, "lxml")
# 商品列表容器(需根据实际页面结构调整)
item_list = soup.select("div.product-item")
return [self._parse_item(item) for item in item_list if item]
def _get_total_pages(self, html: str) -> int:
"""获取总页数"""
soup = BeautifulSoup(html, "lxml")
page_box = soup.select_one(".pagination")
if not page_box:
return 1
# 提取最后一页页码
last_page = page_box.select("a")[-1].text.strip()
return int(last_page) if last_page.isdigit() else 1
def item_search(self,
keyword: str = "",
category: str = "",
price_min: float = None,
price_max: float = None,
style: List[str] = None,
material: str = "",
size_range: str = "",
stock: int = 0,
sort: str = "",
page_limit: int = 5) -> Dict:
"""
搜索服装网商品列表
:param keyword: 搜索关键词
:param category: 分类名称(如“女装-连衣裙”)或分类ID
:param price_min: 最低价格(元)
:param price_max: 最高价格(元)
:param style: 风格列表(如["通勤", "复古"])
:param material: 材质(如“棉”“真皮”)
:param size_range: 尺码范围(如“大码”“均码”)
:param stock: 库存状态(1=现货,2=预售,0=全部)
:param sort: 排序方式(price_asc/sales等)
:param page_limit: 最大页数(默认5)
:return: 标准化搜索结果
"""
try:
# 1. 参数预处理
if not keyword and not category:
return {"success": False, "error_msg": "关键词(keyword)和分类(category)至少需提供一个"}
# 转换分类名称为ID
if category in self.category_map:
cat_id = self.category_map[category]
else:
cat_id = category if category else ""
# 处理风格参数(多风格用逗号分隔并编码)
style_str = ""
if style and len(style) > 0:
encoded_styles = [urllib.parse.quote(s, encoding="utf-8") for s in style]
style_str = ",".join(encoded_styles)
# 编码关键词
encoded_keyword = urllib.parse.quote(keyword, encoding="utf-8") if keyword else ""
all_items = []
current_page = 1
while current_page <= page_limit:
# 构建参数
params = {
"page": current_page
}
if encoded_keyword:
params["keyword"] = encoded_keyword
if cat_id:
params["cat_id"] = cat_id
if price_min is not None:
params["price_min"] = price_min
if price_max is not None:
params["price_max"] = price_max
if style_str:
params["style"] = style_str
if material:
params["material"] = material
if size_range:
params["size_range"] = size_range
if stock in (0, 1, 2):
params["stock"] = stock
if sort:
params["sort"] = sort
# 发送请求(带随机延迟)
time.sleep(random.uniform(2, 4)) # 服装网反爬较严,延迟需适中
headers = self._get_headers()
proxy = self._get_proxy()
response = requests.get(
url=self.base_url,
params=params,
headers=headers,
proxies=proxy,
timeout=10
)
response.raise_for_status()
html = response.text
# 解析当前页商品
items = self._parse_page(html)
if not items:
break # 无数据,终止分页
all_items.extend(items)
# 获取总页数(仅第一页需要)
if current_page == 1:
total_pages = self._get_total_pages(html)
# 修正最大页数(不超过page_limit和50)
total_pages = min(total_pages, page_limit, 50)
if total_pages < current_page:
break
# 若当前页是最后一页,终止
if current_page >= total_pages:
break
current_page += 1
# 去重(基于item_id)
seen_ids = set()
unique_items = []
for item in all_items:
if item["item_id"] not in seen_ids:
seen_ids.add(item["item_id"])
unique_items.append(item)
return {
"success": True,
"total": len(unique_items),
"page_processed": current_page,
"items": unique_items
}
except requests.exceptions.HTTPError as e:
if "403" in str(e):
return {"success": False, "error_msg": "触发反爬,建议更换代理或Cookie", "code": 403}
if "401" in str(e):
return {"success": False, "error_msg": "需要登录,请提供有效Cookie", "code": 401}
return {"success": False, "error_msg": f"HTTP错误: {str(e)}", "code": response.status_code}
except Exception as e:
return {"success": False, "error_msg": f"搜索失败: {str(e)}", "code": -1}
# 使用示例
if __name__ == "__main__":
# 代理池(替换为有效代理)
PROXIES = [
"http://123.45.67.89:8888",
"http://98.76.54.32:8080"
]
# 登录态Cookie(从浏览器获取,用于完整价格)
COOKIE = "session_id=xxx; user_id=xxx"
# 初始化API客户端
search_api = FuzhuangSearchApi(proxy_pool=PROXIES, cookie=COOKIE)
# 搜索“连衣裙”,分类“女装-连衣裙”,价格200-500元,风格通勤+复古,材质棉,仅现货,按销量降序,最多3页
result = search_api.item_search(
keyword="连衣裙",
category="女装-连衣裙",
price_min=200,
price_max=500,
style=["通勤", "复古"],
material="棉",
stock=1,
sort="sales",
page_limit=3
)
if result["success"]:
print(f"搜索成功:共找到 {result['total']} 件商品,处理 {result['page_processed']} 页")
for i, item in enumerate(result["items"][:5]): # 打印前5条
print(f"\n商品 {i+1}:")
print(f"标题:{item['title'][:50]}...") # 截断长标题
print(f"价格:当前¥{item['price']['current']} | 原价¥{item['price']['original']}")
print(f"风格:{', '.join(item['style_tags'])} | 品牌:{item['brand']}")
print(f"规格:颜色{', '.join(item['specs']['colors'][:3])} | 尺码{', '.join(item['specs']['sizes'][:3])}")
print(f"销售:{item['sales']['unit']}{item['sales']['count']}件 | 评分:{item['rating']}分 | 库存:{item['stock_status']}")
print(f"详情页:{item['url']}")
else:
print(f"搜索失败:{result['error_msg']}(错误码:{result.get('code')})")