全部
常见问题
产品动态
精选推荐

用 Python 按关键字搜索 1688 商品:网页版 + 官方 API 双方案(2025-09 亲测可跑)

管理 管理 编辑 删除

在选品、比价、竞品监控里,「先搜后抓」是刚需。今天一篇搞定两种姿势:
① 网页爬虫版——零门槛,5 分钟跑通;
② 官方 API 版——字段最全,企业稳。
全部代码 2025-09 亲测可用,复制即食。



一、技术选型

表格

方案优点场景
网页爬虫requests + BeautifulSoup无需密钥,上手快调研/POC
官方 APIrequests + 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 自动管理)
请登录后查看

one-Jason 最后编辑于2025-09-22 17:20:24

快捷回复
回复
回复
回复({{post_count}}) {{!is_user ? '我的回复' :'全部回复'}}
排序 默认正序 回复倒序 点赞倒序

{{item.user_info.nickname ? item.user_info.nickname : item.user_name}} LV.{{ item.user_info.bbs_level || item.bbs_level }}

作者 管理员 企业

{{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest == 1? '取消推荐': '推荐'}}
{{item.is_suggest == 1? '取消推荐': '推荐'}}
沙发 板凳 地板 {{item.floor}}#
{{item.user_info.title || '暂无简介'}}
附件

{{itemf.name}}

{{item.created_at}}  {{item.ip_address}}
打赏
已打赏¥{{item.reward_price}}
{{item.like_count}}
{{item.showReply ? '取消回复' : '回复'}}
删除
回复
回复

{{itemc.user_info.nickname}}

{{itemc.user_name}}

回复 {{itemc.comment_user_info.nickname}}

附件

{{itemf.name}}

{{itemc.created_at}}
打赏
已打赏¥{{itemc.reward_price}}
{{itemc.like_count}}
{{itemc.showReply ? '取消回复' : '回复'}}
删除
回复
回复
查看更多
打赏
已打赏¥{{reward_price}}
66
{{like_count}}
{{collect_count}}
添加回复 ({{post_count}})

相关推荐

快速安全登录

使用微信扫码登录
{{item.label}} 加精
{{item.label}} {{item.label}} 板块推荐 常见问题 产品动态 精选推荐 首页头条 首页动态 首页推荐
取 消 确 定
回复
回复
问题:
问题自动获取的帖子内容,不准确时需要手动修改. [获取答案]
答案:
提交
bug 需求 取 消 确 定
打赏金额
当前余额:¥{{rewardUserInfo.reward_price}}
{{item.price}}元
请输入 0.1-{{reward_max_price}} 范围内的数值
打赏成功
¥{{price}}
完成 确认打赏

微信登录/注册

切换手机号登录

{{ bind_phone ? '绑定手机' : '手机登录'}}

{{codeText}}
切换微信登录/注册
暂不绑定
CRMEB客服

CRMEB咨询热线 咨询热线

400-8888-794

微信扫码咨询

CRMEB开源商城下载 源码下载 CRMEB帮助文档 帮助文档
返回顶部 返回顶部
CRMEB客服