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

如何用Python爬虫高效获取1688商品详情?

管理 管理 编辑 删除

在电商运营、市场分析和竞品调研中,1688商品详情数据无疑是一座“金矿”。但面对其动态加载、反爬机制,很多开发者望而却步。今天这篇文章,就带你手把手用Python爬虫技术,安全、高效地获取1688商品的核心信息!



一、为什么选择Python爬虫?

1688的商品页面大多采用AJAX动态加载,传统的requests库难以获取完整数据。为此,我们采用:

  • ✅ Selenium:模拟浏览器行为,自动加载JS内容
  • ✅ BeautifulSoup:解析HTML,提取结构化数据
  • ✅ Pandas(可选):数据清洗与导出Excel


二、前期准备

1. 安装依赖库

bash

pip install selenium beautifulsoup4 pandas webdriver-manager

2. 下载ChromeDriver(自动管理)

Python

from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver

driver = webdriver.Chrome(ChromeDriverManager().install())


三、实战案例:抓取1688商品详情页

我们以如下商品详情页为例:https://detail.1688.com/offer/123456789.html


✅ 步骤1:模拟访问并滚动加载页面

Python

import time
from selenium import webdriver
from bs4 import BeautifulSoup

def scroll_to_bottom(driver):
    last_height = driver.execute_script("return document.body.scrollHeight")
    while True:
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        time.sleep(2)
        new_height = driver.execute_script("return document.body.scrollHeight")
        if new_height == last_height:
            break
        last_height = new_height

url = "https://detail.1688.com/offer/123456789.html"
driver.get(url)
scroll_to_bottom(driver)


✅ 步骤2:解析商品核心信息

Python

soup = BeautifulSoup(driver.page_source, 'html.parser')

product_info = {
    'name': soup.select_one('h1.d-title').text.strip(),
    'price': soup.select_one('.price-original').text.strip(),
    'image': soup.select_one('#dt-tab img')['src'],
    'description': soup.select_one('.detail-content').text.strip()[:200] + '...'
}

print("商品名称:", product_info['name'])
print("商品价格:", product_info['price'])
print("商品图片:", product_info['image'])
print("商品描述:", product_info['description'])


四、进阶:使用1688官方API(更稳定)

如果你希望更合规、更结构化地获取数据,可以申请1688开放平台账号,使用官方API:

Python

import requests, time, hashlib

def generate_sign(params, app_secret):
    sorted_params = sorted(params.items())
    sign_str = "&".join([f"{k}{v}" for k, v in sorted_params if k != "sign"])
    return hashlib.md5((sign_str + app_secret).encode('utf-8')).hexdigest().upper()

params = {
    "method": "alibaba.product.get",
    "app_key": "YOUR_APP_KEY",
    "product_id": "123456789",
    "timestamp": str(int(time.time() * 1000)),
    "format": "json",
    "v": "2.0"
}
params["sign"] = generate_sign(params, "YOUR_APP_SECRET")

response = requests.get("https://gw.open.1688.com/openapi/param2/2/portals.open/api/", params=params)
data = response.json()

if data.get("code") == "0":
    product = data["result"]["productInfo"]
    print("商品标题:", product["subject"])
    print("价格区间:", product["priceRange"])
    print("最小起订量:", product["moq"])
    


五、注意事项 & 反爬建议

  • ✅ 设置请求间隔:每请求一次页面,sleep 1~3秒
  • ✅ 随机User-Agent:避免被识别为机器人
  • ✅ 使用代理IP:高并发时建议使用代理池
  • ✅ 遵守robots.txt:尊重网站的爬虫协议
  • ✅ 合理存储数据:避免敏感信息泄露


六、总结

通过本文,你学会了:

  • 如何用Selenium抓取动态加载的1688商品详情页
  • 如何用BeautifulSoup提取商品标题、价格、图片、描述
  • 如何接入1688官方API,获取更完整、合规的数据
  • 无论是做竞品分析、价格监控、还是选品上架,这套方案都能为你提供强大支持!
请登录后查看

one-Jason 最后编辑于2025-09-19 16:59:26

快捷回复
回复
回复
回复({{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}}
76
{{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客服