全部
常见问题
产品动态
精选推荐

如何使用Python通过API接口批量抓取小红书笔记评论?

管理 管理 编辑 删除

一、技术可行性分析

  1. API存在性验证 小红书官方未公开评论API,需通过逆向工程获取接口 典型评论接口特征:参考小红书开放平台API接口
  2. 请求参数解析 csharp 体验AI代码助手 代码解读复制代码python # 典型请求参数示例 params = { "note_id": "64a1b2c3d4e5f6g7h8i9j0k1", # 笔记ID "sort": "newest", # 排序方式 "page": 1, # 页码 "page_size": 20 # 每页数量 }

二、完整技术实现方案

python
import requests
import json
import time
from urllib.parse import urlencode
 
class XiaohongshuCommentScraper:
    def __init__(self):
        self.session = requests.Session()
        self.headers = {
            "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 16_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148",
            "X-Request-ID": "api.comment_list",
            "Content-Type": "application/json"
        }
        self.base_url = "https://www.xiaohongshu.com/api/sns/note/v1/comment_list"
 
    def get_comments(self, note_id, max_pages=5):
        comments = []
        for page in range(1, max_pages+1):
            params = {
                "note_id": note_id,
                "sort": "newest",
                "page": page,
                "page_size": 20
            }
            
            try:
                response = self.session.get(
                    self.base_url, 
                    headers=self.headers,
                    params=urlencode(params)
                )
                data = response.json()
                
                if data.get('errcode') != 0:
                    raise Exception(f"API Error: {data.get('errmsg')}")
                
                page_comments = data.get('data', {}).get('comments', [])
                if not page_comments:
                    break
                    
                comments.extend([{
                    "id": c["id"],
                    "user": c["user"]["nickname"],
                    "content": c["content"],
                    "like_count": c["like_count"],
                    "create_time": c["create_time"]
                } for c in page_comments])
                
                time.sleep(1.5)  # 反爬延迟
                
            except Exception as e:
                print(f"Error on page {page}: {str(e)}")
                break
                
        return comments
 
if __name__ == "__main__":
    scraper = XiaohongshuCommentScraper()
    note_id = "64a1b2c3d4e5f6g7h8i9j0k1"  # 替换为实际笔记ID
    
    comments = scraper.get_comments(note_id, max_pages=3)
    print(f"成功抓取 {len(comments)} 条评论")
    
    # 保存为CSV文件
    with open("comments.csv", "w", encoding="utf-8") as f:
        f.write("ID,用户,内容,点赞数,创建时间\n")
        for c in comments:
            f.write(f"{c['id']},{c['user']},{c['content']},{c['like_count']},{c['create_time']}\n")

三、进阶优化方案

  1. 分布式爬取架构 python 体验AI代码助手 代码解读复制代码python from concurrent.futures import ThreadPoolExecutor def distributed_scraping(note_ids): with ThreadPoolExecutor(max_workers=5) as executor: futures = [executor.submit(scraper.get_comments, nid, 2) for nid in note_ids] results = [f.result() for f in futures] return results
  2. 反爬对抗策略 ruby 体验AI代码助手 代码解读复制代码python class AntiCrawler: @staticmethod def get_proxy(): # 返回可用代理IP return {"http": "123.45.67.89:8080"} @staticmethod def random_delay(): time.sleep(random.uniform(1, 3))

四、数据存储扩展

python
import sqlite3
 
class DataStorage:
    def __init__(self):
        self.conn = sqlite3.connect("comments.db")
        self._create_table()
        
    def _create_table(self):
        cursor = self.conn.cursor()
        cursor.execute('''
            CREATE TABLE IF NOT EXISTS comments (
                id TEXT PRIMARY KEY,
                note_id TEXT,
                user TEXT,
                content TEXT,
                like_count INTEGER,
                create_time INTEGER
            )
        ''')
        self.conn.commit()
        
    def save_comments(self, comments, note_id):
        cursor = self.conn.cursor()
        for c in comments:
            cursor.execute('''
                INSERT OR REPLACE INTO comments 
                VALUES (?, ?, ?, ?, ?, ?)
            ''', (c["id"], note_id, c["user"], c["content"], c["like_count"], c["create_time"]))
        self.conn.commit()

五、法律与道德规范

  1. 遵守《网络安全法》和《数据安全法》
  2. 禁止抓取用户隐私信息(手机号、地址等)
  3. 控制请求频率(建议QPS≤2)
  4. 明确标注数据来源,禁止商业用途

该方案通过模拟移动端请求实现评论抓取,采用分页机制和动态延迟策略规避反爬限制。实际部署时建议配合代理IP池和异常重试机制,确保数据采集的稳定性和合规性。


请登录后查看

OneLafite 最后编辑于2025-07-08 14:49:15

快捷回复
回复
回复
回复({{post_count}}) {{!is_user ? '我的回复' :'全部回复'}}
排序 默认正序 回复倒序 点赞倒序

{{item.user_info.nickname ? item.user_info.nickname : item.user_name}} LV.{{ item.user_info.bbs_level || item.bbs_level }}

作者 管理员 企业

{{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest == 1? '取消推荐': '推荐'}}
{{item.is_suggest == 1? '取消推荐': '推荐'}}
沙发 板凳 地板 {{item.floor}}#
{{item.user_info.title || '暂无简介'}}
附件

{{itemf.name}}

{{item.created_at}}  {{item.ip_address}}
打赏
已打赏¥{{item.reward_price}}
{{item.like_count}}
{{item.showReply ? '取消回复' : '回复'}}
删除
回复
回复

{{itemc.user_info.nickname}}

{{itemc.user_name}}

回复 {{itemc.comment_user_info.nickname}}

附件

{{itemf.name}}

{{itemc.created_at}}
打赏
已打赏¥{{itemc.reward_price}}
{{itemc.like_count}}
{{itemc.showReply ? '取消回复' : '回复'}}
删除
回复
回复
查看更多
打赏
已打赏¥{{reward_price}}
34
{{like_count}}
{{collect_count}}
添加回复 ({{post_count}})

相关推荐

快速安全登录

使用微信扫码登录
{{item.label}} 加精
{{item.label}} {{item.label}} 板块推荐 常见问题 产品动态 精选推荐 首页头条 首页动态 首页推荐
取 消 确 定
回复
回复
问题:
问题自动获取的帖子内容,不准确时需要手动修改. [获取答案]
答案:
提交
bug 需求 取 消 确 定
打赏金额
当前余额:¥{{rewardUserInfo.reward_price}}
{{item.price}}元
请输入 0.1-{{reward_max_price}} 范围内的数值
打赏成功
¥{{price}}
完成 确认打赏

微信登录/注册

切换手机号登录

{{ bind_phone ? '绑定手机' : '手机登录'}}

{{codeText}}
切换微信登录/注册
暂不绑定
CRMEB客服

CRMEB咨询热线 咨询热线

400-8888-794

微信扫码咨询

CRMEB开源商城下载 源码下载 CRMEB帮助文档 帮助文档
返回顶部 返回顶部
CRMEB客服