Python采集京东商品详情数据
要使用Python采集京东商品详情数据,主要有两种方法:通过京东官方API接口或通过网页爬虫技术。以下是两种方法的实现方式:
方法一:使用京东官方API接口
京东提供了部分开放的API接口,但大多数商业API需要申请权限。以下是使用京东联盟API获取商品详情的示例:
import requests | |
import hashlib | |
import time | |
import json | |
def jd_api_request(app_key, app_secret, sku_id): | |
# 京东API参数 | |
method = 'jingdong.ware.detail.get' | |
timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) | |
format = 'json' | |
v = '2.0' | |
# 构造签名 | |
sign_str = f'{app_key}{method}{timestamp}{format}{v}{sku_id}{app_secret}' | |
sign = hashlib.md5(sign_str.encode('utf-8')).hexdigest().upper() | |
# 请求参数 | |
params = { | |
'method': method, | |
'app_key': app_key, | |
'sign': sign, | |
'timestamp': timestamp, | |
'format': format, | |
'v': v, | |
'sku_id': sku_id | |
} | |
# 发送请求 | |
url = 'https://api.jd.com/routerjson' | |
response = requests.get(url, params=params) | |
return response.json() | |
# 使用示例 | |
app_key = '你的APP_KEY' # 需要申请京东联盟开发者账号获取 | |
app_secret = '你的APP_SECRET' | |
sku_id = '商品ID' # 如: 100000000001 | |
result = jd_api_request(app_key, app_secret, sku_id) | |
print(json.dumps(result, indent=2, ensure_ascii=False)) |
注意:
- 需要先注册账号
- 申请API权限并获取app_key和app_secret
- 大多数API有调用频率限制
方法二:通过网页爬虫获取商品详情
如果无法获取官方API权限,可以通过分析京东网页结构来爬取数据:
import requests | |
from bs4 import BeautifulSoup | |
import re | |
import json | |
def get_jd_product_detail(sku_id): | |
headers = { | |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36', | |
'Referer': f'https://item.jd.com/{sku_id}.html' | |
} | |
# 获取商品页面 | |
url = f'https://item.jd.com/{sku_id}.html' | |
response = requests.get(url, headers=headers) | |
response.encoding = 'utf-8' | |
if response.status_code != 200: | |
return None | |
# 解析页面 | |
soup = BeautifulSoup(response.text, 'html.parser') | |
# 提取商品名称 | |
title = soup.find('div', class_='sku-name').get_text(strip=True) | |
# 提取价格 - 京东价格通常通过AJAX加载 | |
price_url = f'https://p.3.cn/prices/mgets?skuIds=J_{sku_id}' | |
price_response = requests.get(price_url, headers=headers) | |
price_data = price_response.json() | |
price = price_data[0].get('p', '0.00') | |
# 提取商品图片 | |
img_url = soup.find('div', class_='spec-img').find('img')['src'] | |
# 提取商品详情描述 - 通常在script标签中 | |
script_text = soup.find('script', text=re.compile('window.PRODUCT_DETAIL')).string | |
if script_text: | |
# 提取JSON数据 | |
json_str = re.search(r'window\.PRODUCT_DETAIL\s*=\s*({.*?});', script_text, re.DOTALL).group(1) | |
product_detail = json.loads(json_str) | |
# 提取更多信息 | |
shop_name = product_detail.get('shopName', '') | |
category = product_detail.get('category', '') | |
description = product_detail.get('description', '') | |
else: | |
product_detail = {} | |
shop_name = category = description = '' | |
# 返回结果 | |
return { | |
'title': title, | |
'price': price, | |
'image_url': img_url, | |
'sku_id': sku_id, | |
'shop_name': shop_name, | |
'category': category, | |
'description': description, | |
'detail_data': product_detail | |
} | |
# 使用示例 | |
sku_id = '100000000001' # 替换为实际商品ID | |
product_info = get_jd_product_detail(sku_id) | |
print(json.dumps(product_info, indent=2, ensure_ascii=False)) |