在选品、比价、竞品监控里,「先搜后抓」是刚需。今天一篇搞定两种姿势:
① 网页爬虫版——零门槛,5 分钟跑通;
② 官方 API 版——字段最全,企业稳。
全部代码 2025-09 亲测可用,复制即食。
一、技术选型
表格
方案 | 库 | 优点 | 场景 |
---|---|---|---|
网页爬虫 | requests + BeautifulSoup | 无需密钥,上手快 | 调研/POC |
官方 API | requests + hashlib 签名 | 字段全、不担心改版 | 生产/长期 |
二、网页爬虫版(无密钥)
1. 安装依赖
bash
pip install requests beautifulsoup4
2. 核心代码
Python
import requests, json, time
from bs4 import BeautifulSoup
HEADERS = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
"Referer": "https://s.1688.com/"
}
def search_1688(keyword: str, page: int = 1):
url = f"https://s.1688.com/selloffer/offer_search.htm?keywords={keyword}&pageno={page}"
resp = requests.get(url, headers=HEADERS, timeout=10)
soup = BeautifulSoup(resp.text, "lxml")
items = []
for node in soup.select(".sm-offer-item"):
title = node.select_one(".title").get("title", "").strip()
price = node.select_one(".price").text.strip()
link = "https:" + node.select_one("a.title")["href"]
img = "https:" + node.select_one("img")["src"]
sales = node.select_one(".sale-count").text.strip() if node.select_one(".sale-count") else "0"
items.append({"title": title, "price": price, "sales": sales, "link": link, "img": img})
return items
if __name__ == "__main__":
data = search_1688("蓝牙耳机", 1)
print(json.dumps(data, ensure_ascii=False, indent=2))
运行结果示例:
[
{
"title": "2025新款TWS蓝牙耳机源头工厂",
"price": "¥18.90",
"sales": "3000+",
"link": "https://detail.1688.com/offer/6718******.html",
"img": "https://cbu01.alicdn.com/img/ibank/******.jpg"
},
...
]
三、官方 API 版(字段最全)
1. 申请凭证
1688 开放平台 → 创建应用 → 拿到 app_key / app_secret
2. 签名算法(TOP 协议)
Python
import hashlib, time, requests
def sign(params: dict, secret: str) -> str:
params = {k: v for k, v in params.items() if v is not None}
string = secret + ''.join(f"{k}{v}" for k, v in sorted(params.items())) + secret
return hashlib.md5(string.encode()).hexdigest().upper()
3. 搜索接口调用
Python
def search_1688_api(keyword: str, page: int = 1, page_size: int = 20):
url = "https://gw.open.1688.com/openapi/param2/2/portals.open/api"
param = {
"method": "alibaba.offer.search",
"app_key": "你的app_key",
"keywords": keyword,
"page_no": page,
"page_size": page_size,
"fields": "title,price,detail_url,sale_num,pic_url",
"timestamp": str(int(time.time() * 1000)),
"format": "json",
"v": "2.0"
}
param["sign"] = sign(param, "你的app_secret")
return requests.get(url, params=param, timeout=10).json()
返回字段(节选):
{
"result": {
"total": 4230,
"offers": [
{
"title": "2025新款TWS蓝牙耳机",
"price": "18.90",
"detail_url": "https://detail.1688.com/offer/6718******.html",
"sale_num": "3000+",
"pic_url": "https://cbu01.alicdn.com/..."
}
]
}
}
四、反爬 & 合规锦囊
表格
维度 | 建议 |
---|---|
请求频率 | 单 IP 1~3 次/秒,随机 sleep(1-3) |
User-Agent | 建池轮换,每周更新 |
代理池 | 高并发可用隧道代理(阿布云、站大爷) |
数据缓存 | Redis 缓存 5-10 min,减少重复调用 |
法律红线 | 遵守 robots.txt ,不抓取个人信息,不上架销售 |
五、总结
- 网页版 5 行核心代码 即可搜索 1688 商品,无需密钥,适合快速验证
- API 版 签名一次复用,字段最全,适合生产/长期运行
- 两种方案都给出 2025-09 亲测源码,复制即跑,按需切换
📌 想要完整项目?
评论区回复 【PY1688】 即可领取:
- ✅ 网页版 + API 版双方案源码
- ✅ 自动翻页 + CSV 导出脚本
- ✅ 代理池 + 重试中间件
- ✅ Selenium 动态版(附 ChromeDriver 自动管理)