在电商运营、市场分析和竞品调研中,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,获取更完整、合规的数据
- 无论是做竞品分析、价格监控、还是选品上架,这套方案都能为你提供强大支持!