import requests
import json
# 假设API封装接口地址
API url=c0b.cc/R4rbK2
def get_shopee_item_detail(shop_id, item_id, country_code='my'):
"""
获取Shopee平台上指定商品的详情信息
参数:
shop_id (int): 店铺ID
item_id (int): 商品ID
country_code (str): 国家代码,默认为马来西亚('my')
返回:
dict: 商品详情信息的JSON解析结果
"""
# 根据国家代码选择对应的API域名
domain_map = {
'my': 'shopee.com.my', # 马来西亚
'sg': 'shopee.sg', # 新加坡
'id': 'shopee.co.id', # 印尼
'th': 'shopee.co.th', # 泰国
'vn': 'shopee.vn', # 越南
'ph': 'shopee.ph', # 菲律宾
'tw': 'shopee.tw' # 台湾
}
base_domain = domain_map.get(country_code, 'shopee.com.my')
api_url = f"https://{base_domain}/api/v4/item/get"
# 设置请求头,模拟浏览器访问
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',
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Referer': f'https://{base_domain}/',
'Origin': f'https://{base_domain}',
'Sec-Fetch-Site': 'same-site',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Dest': 'empty',
}
# 设置请求参数
params = {
'itemid': item_id,
'shopid': shop_id,
}
try:
# 发送请求
response = requests.get(api_url, headers=headers, params=params)
# 检查响应状态码
if response.status_code == 200:
# 解析JSON数据
data = response.json()
return data
else:
print(f"请求失败,状态码: {response.status_code}")
print(f"错误信息: {response.text}")
return None
except requests.exceptions.RequestException as e:
print(f"请求发生异常: {e}")
return None
# 使用示例
if __name__ == "__main__":
# 示例店铺ID和商品ID(需要替换为实际有效的ID)
shop_id = 123456
item_id = 7890123
# 获取商品详情
item_detail = get_shopee_item_detail(shop_id, item_id)
# 打印结果
if item_detail:
print(f"商品标题: {item_detail.get('data', {}).get('name', '未知')}")
print(f"商品价格: {item_detail.get('data', {}).get('price_min', '未知') / 100000}")
print(f"库存数量: {item_detail.get('data', {}).get('stock', '未知')}")
print(json.dumps(item_detail, indent=2, ensure_ascii=False))
评论