×

python采集速卖通商品评论API接口系列,json数据返回

知名用户18007905473 知名用户18007905473 发表于2025-10-29 14:56:33 浏览28 评论0

抢沙发发表评论

以下是采集速卖通商品评论的 Python 代码示例,通过调用相关 API 接口获取评论数据并以 JSON 格式返回。代码实现了分页获取评论、处理不同类型评论(带图 / 好评 / 中评 / 差评)等功能:
python
运行
import requestsimport jsonimport timefrom fake_useragent import UserAgentfrom random import randomclass AliExpressCommentScraper:
    def __init__(self):
        # 初始化请求头,模拟移动端访问
        self.headers = {
            'User-Agent': UserAgent().random,
            'Accept': 'application/json, text/plain, */*',
            'Accept-Language': 'en-US,en;q=0.9',
            'Referer': 'https://m.aliexpress.com/',
            'X-Requested-With': 'XMLHttpRequest',
        }
        # 评论API基础URL(移动端接口)
        self.comment_api_url = "https://feedback.aliexpress.com/pc/searchEvaluation.do"

    def get_product_comments(self, product_id, page=1, page_size=20, comment_type=0):
        """
        获取商品评论数据
        :param product_id: 商品ID
        :param page: 页码(从1开始)
        :param page_size: 每页评论数(最大20)
        :param comment_type: 评论类型 0-全部 1-好评 2-中评 3-差评 4-带图
        :return: 评论数据JSON
        """
        try:
            # 构造请求参数
            params = {
                'productId': product_id,
                'page': page,
                'pageSize': page_size,
                'starFilter': comment_type,  # 评论类型筛选
                'sort': 0,  # 排序方式 0-默认 1-最新 2-评分高 3-有图
                'translate': 'Y',  # 自动翻译评论
                '_ksTS': self._generate_ks_ts(),  # 速卖通API时间戳参数
                'callback': f'jsonp_{int(time.time() * 1000)}_{int(random() * 1000)}'
            }

            # 发送请求(注意:速卖通评论API返回的是JSONP格式,需要处理)
            response = requests.get(
                url=self.comment_api_url,
                params=params,
                headers=self.headers,
                timeout=15
            )

            if response.status_code == 200:
                # 处理JSONP格式(去掉前后的回调函数包装)
                jsonp_data = response.text.strip()
                if jsonp_data.startswith('jsonp_') and jsonp_data.endswith(')'):
                    json_str = jsonp_data[jsonp_data.index('(') + 1 : -1]
                    comment_data = json.loads(json_str)

                    # 提取有用的评论信息
                    if comment_data.get('success'):
                        result = {
                            'product_id': product_id,
                            'total_comments': comment_data.get('totalValidNum', 0),  # 总评论数
                            'total_pages': comment_data.get('totalPage', 0),  # 总页数
                            'current_page': page,
                            'comment_type': comment_type,
                            'comments': []
                        }

                        # 解析每条评论
                        for item in comment_data.get('evaList', []):
                            comment_item = {
                                'comment_id': item.get('evaId'),
                                'buyer_name': item.get('userName'),
                                'buyer_country': item.get('country', '').upper(),
                                'rating': item.get('star'),  # 评分(1-5)
                                'comment_time': item.get('createTime'),
                                'comment_content': item.get('evalContent'),  # 原始评论
                                'translated_content': item.get('translateContent'),  # 翻译后评论
                                'product_variant': item.get('skuInfo'),  # 购买的商品变体
                                'order_time': item.get('orderTime'),
                                'useful_votes': item.get('helpful'),  # 有用数
                                'images': [img.get('url') for img in item.get('picList', [])]  # 评论图片
                            }
                            result['comments'].append(comment_item)

                        return json.dumps(result, ensure_ascii=False, indent=2)
                    else:
                        return json.dumps({
                            'error': '获取评论失败',
                            'message': comment_data.get('message', '未知错误')
                        }, indent=2)
                else:
                    return json.dumps({'error': '数据格式错误,非JSONP'}, indent=2)
            else:
                return json.dumps({'error': f'请求失败,状态码: {response.status_code}'}, indent=2)

        except Exception as e:
            return json.dumps({'error': '发生异常', 'message': str(e)}, indent=2)

    def _generate_ks_ts(self):
        """生成速卖通API所需的_ksTS参数"""
        timestamp = int(time.time() * 1000)
        return f'{timestamp}_234'

    def get_all_comments(self, product_id, max_pages=5, comment_type=0):
        """
        批量获取多页评论
        :param product_id: 商品ID
        :param max_pages: 最大获取页数
        :param comment_type: 评论类型
        :return: 所有评论汇总的JSON
        """
        all_comments = []
        total_pages = 1
        current_page = 1

        while current_page <= total_pages and current_page <= max_pages:
            print(f'获取第 {current_page} 页评论...')
            page_data = self.get_product_comments(product_id, current_page, comment_type=comment_type)
            if not page_data:
                break

            page_json = json.loads(page_data)
            if 'error' in page_json:
                print(f'错误: {page_json["message"]}')
                break

            all_comments.extend(page_json.get('comments', []))
            total_pages = page_json.get('total_pages', 1)
            current_page += 1

            # 避免请求过于频繁,添加随机间隔
            time.sleep(1 + random() * 2)

        result = {
            'product_id': product_id,
            'total_comments_fetched': len(all_comments),
            'comment_type': comment_type,
            'comments': all_comments        }
        return json.dumps(result, ensure_ascii=False, indent=2)if __name__ == "__main__":
    # 示例用法
    scraper = AliExpressCommentScraper()
    product_id = "1005005762792526"  # 替换为实际商品ID

    # 获取第1页全部评论
    print("===== 第1页全部评论 =====")
    print(scraper.get_product_comments(product_id, page=1))

    # 获取带图评论(前3页)
    print("\n===== 带图评论(前3页) =====")
    print(scraper.get_all_comments(product_id, max_pages=3, comment_type=4))

    # 获取差评(前2页)
    print("\n===== 差评(前2页) =====")
    print(scraper.get_all_comments(product_id, max_pages=2, comment_type=3))

使用说明

  1. 安装依赖
    bash
    pip install requests fake_useragent
  2. 核心功能
    • get_product_comments():获取单页评论,支持筛选评论类型(全部 / 好评 / 中评 / 差评 / 带图)

    • get_all_comments():批量获取多页评论,自动处理分页逻辑

    • 支持提取评论内容、评分、买家国家、评论图片、购买变体等详细信息

  3. 参数说明
    • product_id:商品 ID(从速卖通商品详情页 URL 中获取,如https://www.aliexpress.com/item/1005005762792526.html中的1005005762792526

    • comment_type:评论类型筛选(0 - 全部,1 - 好评,2 - 中评,3 - 差评,4 - 带图)

    • max_pages:最大获取页数(避免请求过多被限制)

注意事项

  1. 反爬机制
    • 代码中添加了随机 User-Agent 和请求间隔,降低被识别为爬虫的风险

    • 过于频繁的请求可能导致 IP 被临时封禁,建议控制请求频率

    • 可根据需要添加代理 IP 池进一步规避限制

  2. API 稳定性
    • 速卖通 API 可能不定期更新,若接口失效需检查参数或 URL 是否变化

    • JSONP 格式处理可能需要根据实际返回调整

  3. 合规性
    • 商业使用请遵守速卖通的用户协议和 robots.txt 规则

    • 避免将采集数据用于非法用途或侵犯他人权益

如果需要获取更多字段(如评论者购买数量、物流评分等),可以解析 API 返回的完整 JSON 数据(通过打印comment_data查看),然后在comment_item中添加相应字段映射。


群贤毕至

访客