首页 > 大数据 > 数据挖掘 > 获取各个电商平台常用的一些API
各个电商平台常用的API种类繁多,涵盖了商品、订单、支付、营销、用户等多个方面。以下是一些主流电商平台(如淘宝、京东、拼多多等)的常用API接口示例,包括代码及调用步骤。但请注意,由于API可能会随着平台升级而发生变化,因此以下信息可能需要根据实际情况进行调整。
python复制代码
|
|
import requests |
|
|
import json |
|
|
|
|
|
# 淘宝API接口地址(需替换为真实可用的接口) |
|
|
api_url = "https://eco.taobao.com/router/rest" |
|
|
|
|
|
# 请求参数(需根据API要求设置,包括app_key、app_secret、timestamp等,并生成签名) |
|
|
params = { |
|
|
"method": "taobao.tbk.item.get", |
|
|
"app_key": "YOUR_APP_KEY", |
|
|
"app_secret": "YOUR_APP_SECRET", |
|
|
"format": "json", |
|
|
"v": "2.0", |
|
|
"fields": "num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick", |
|
|
"q": "搜索关键词", # 替换为实际搜索关键词 |
|
|
"page_no": "1", |
|
|
"page_size": "20", |
|
|
# 其他参数如timestamp、sign等需根据平台文档生成 |
|
|
} |
|
|
|
|
|
# 发送请求并解析响应 |
|
|
response = requests.get(api_url, params=params) |
|
|
if response.status_code == 200: |
|
|
data = response.json() |
|
|
# 处理响应数据,如提取商品信息 |
|
|
items = data.get("tbk_item_get_response", {}).get("results", {}).get("n_tbk_item", []) |
|
|
for item in items: |
|
|
print(f"商品ID: {item.get('num_iid')}, 商品标题: {item.get('title')}") |
|
|
else: |
|
|
print(f"请求失败,状态码:{response.status_code}") |
调用步骤:
python复制代码
|
|
import requests |
|
|
import hashlib |
|
|
import hmac |
|
|
import time |
|
|
import base64 |
|
|
import urllib.parse |
|
|
|
|
|
# 京东商品详情API的URL |
|
|
api_url = "https://router.jd.com/api" |
|
|
|
|
|
# 商品ID(SKU) |
|
|
sku = "1234567890" # 替换为实际的商品ID或SKU |
|
|
|
|
|
#
|
|
|
app_key = "YOUR_APP_KEY" |
|
|
app_secret = "YOUR_APP_SECRET" |
|
|
|
|
|
# 生成签名 |
|
|
def generate_sign(params, app_secret): |
|
|
params_str = "&".join(["{}={}".format(urllib.parse.quote(k), urllib.parse.quote(v)) for k, v in sorted(params.items())]) |
|
|
sign_content = app_secret + params_str + app_secret |
|
|
sign = base64.b64encode(hmac.new(sign_content.encode('utf-8'), digestmod=hashlib.md5).digest()).decode('utf-8') |
|
|
return sign |
|
|
|
|
|
# 构建请求参数 |
|
|
params = { |
|
|
"method": "jingdong.ware.product.get", |
|
|
"app_key": app_key, |
|
|
"timestamp": str(int(time.time() * 1000)), # 毫秒级时间戳 |
|
|
"v": "2.0", |
|
|
"sku": sku, |
|
|
} |
|
|
|
|
|
# 生成签名并添加到请求参数中 |
|
|
params["sign"] = generate_sign(params, app_secret) |
|
|
|
|
|
# 发送请求 |
|
|
response = requests.get(api_url, params=params) |
|
|
|
|
|
# 解析返回的JSON数据 |
|
|
data = response.json() |
|
|
|
|
|
# 提取商品详情信息 |
|
|
product = data.get("jingdong_ware_product_get_response", {}).get("ware_product_dto", {}) |
|
|
if product: |
|
|
print("商品标题:", product.get("product_name")) |
|
|
print("商品价格:", product.get("price")) |
|
|
print("商品库存:", product.get("stock_num")) |
|
|
print("商品图片链接:", product.get("image_url")) |
|
|
else: |
|
|
print("获取商品详情失败") |
调用步骤:
python复制代码
|
|
import requests |
|
|
import json |
|
|
import time |
|
|
import hashlib |
|
|
import hmac |
|
|
import base64 |
|
|
import urllib.parse |
|
|
|
|
|
# 拼多多API接口地址 |
|
|
api_url = "https://gw-api.pinduoduo.com/api/router" |
|
|
|
|
|
# 商品ID列表 |
|
|
goods_id_list = "123,456,789" # 替换为实际的商品ID列表 |
|
|
|
|
|
# 获取appKey和appSecret(在实际使用中需要替换成自己的值) |
|
|
app_key = "YOUR_APP_KEY" |
|
|
app_secret = "YOUR_APP_SECRET" |
|
|
|
|
|
# 生成签名 |
|
|
def generate_sign(params, app_secret): |
|
|
params_str = "&".join(["{}={}".format(urllib.parse.quote(k), urllib.parse.quote(v)) for k, v in sorted(params.items())]) |
|
|
sign_content = app_secret + params_str + app_secret |
|
|
sign = base64.b64encode(hmac.new(sign_content.encode('utf-8'), digestmod=hashlib.md5).digest()).decode('utf-8') |
|
|
return sign |
|
|
|
|
|
# 构建请求参数 |
|
|
params = { |
|
|
"type": "pdd.ddk.goods.detail.increment.get", |
|
|
"app_key": app_key, |
|
|
"timestamp": str(int(time.time())), |
|
|
"goods_id_list": goods_id_list, |
|
|
"sign_method": "md5", |
|
|
} |
|
|
|
|
|
# 生成签名并添加到请求参数中 |
|
|
params["sign"] = generate_sign(params, app_secret) |
|
|
|
|
|
# 构造请求URL |
|
|
url = api_url + "?" + urllib.parse.urlencode(params) |
|
|
|
|
|
# 发送GET请求 |
|
|
response = requests.get(url) |
|
|
|
|
|
# 处理响应数据 |
|
|
if response.status_code == 200: |
|
|
data = response.json() |
|
|
if data['success']: |
|
|
goods_list = data['result']['goods_detail_list'] |
|
|
for goods in goods_list: |
|
|
print('商品ID:', goods['goods_id']) |
|
|
print('商品标题:', goods['goods_name']) |
|
|
print('商品价格:', goods['goods_price']) |
|
|
print('商品销量:', goods['sales']) |
|
|
else: |
|
|
print('请求失败:', data['error_msg']) |
|
|
else: |
|
|
print('请求异常:', response.status_code) |
调用步骤:
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70042344/viewspace-3066426/ ,如需转载,请注明出处,否则将追究法律责任。