小红书笔记详情API接口系列,json数据返回

小红书笔记详情API接口分析

小红书的笔记详情数据可以通过多个API接口获取,以下是常见的API接口系列及返回的JSON数据结构分析。需要注意的是,这些API接口可能会随时间变化而调整。

1. 笔记基础信息API

接口URL

bash 复制代码
https://www.xiaohongshu.com/api/sns/v1/note/{note_id}

请求方式

GET

请求头

java 复制代码
python
headers = {
    "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1",
    "X-Sign": "X...(动态生成)",
    "Cookie": "你的有效cookie"
}

返回JSON示例

json 复制代码
json
{
    "id": "5f8d3a3a000000000102e3a1",
    "title": "超详细的护肤步骤分享",
    "desc": "今天来分享一下我的日常护肤步骤...",
    "type": "normal",
    "likes": 1562,
    "collects": 892,
    "comments": 128,
    "shareCount": 45,
    "time": "2020-10-20 14:30:00",
    "user": {
        "userid": "5d3e6b2a000000000101c3f2",
        "nickname": "护肤小达人",
        "image": "https://sns-avatar-qc.xhscdn.com/avatar/5d3e6b2a000000000101c3f2.jpg",
        "redOfficial": false
    },
    "images": [
        {
            "url": "https://ci.xiaohongshu.com/5f8d3a3a000000000102e3a1",
            "width": 1080,
            "height": 1350,
            "fileid": "5f8d3a3a000000000102e3a1"
        }
    ],
    "tags": [
        {
            "id": "5d3e6b2a000000000101c3f3",
            "name": "护肤"
        }
    ],
    "ipLocation": "上海"
}

2. 笔记详细内容API

接口URL

bash 复制代码
https://www.xiaohongshu.com/api/sns/v2/note/{note_id}/detail

返回JSON示例

json 复制代码
json
{
    "note": {
        "id": "5f8d3a3a000000000102e3a1",
        "title": "超详细的护肤步骤分享",
        "desc": "今天来分享一下我的日常护肤步骤...",
        "displayTitle": "",
        "richText": [
            {
                "type": "text",
                "content": "第一步:清洁",
                "style": {
                    "bold": true,
                    "fontSize": 18
                }
            },
            {
                "type": "image",
                "url": "https://ci.xiaohongshu.com/5f8d3a3a000000000102e3a1",
                "width": 1080,
                "height": 1350
            }
        ],
        "goods": [
            {
                "id": "5d3e6b2a000000000101c3f4",
                "name": "某品牌洗面奶",
                "price": "128",
                "image": "https://ci.xiaohongshu.com/5d3e6b2a000000000101c3f4"
            }
        ]
    },
    "interactions": {
        "liked": false,
        "collected": false,
        "followed": false
    }
}

3. 笔记评论API

接口URL

bash 复制代码
https://www.xiaohongshu.com/api/sns/v1/note/{note_id}/comments

请求参数

  • page: 页码
  • page_size: 每页数量

返回JSON示例

json 复制代码
json
{
    "comments": [
        {
            "id": "5f8d3a3a000000000102e3a2",
            "content": "太详细了,收藏学习!",
            "time": "2020-10-20 15:30:00",
            "likes": 25,
            "user": {
                "userid": "5d3e6b2a000000000101c3f5",
                "nickname": "美妆爱好者",
                "image": "https://sns-avatar-qc.xhscdn.com/avatar/5d3e6b2a000000000101c3f5.jpg"
            },
            "subComments": [
                {
                    "id": "5f8d3a3a000000000102e3a3",
                    "content": "谢谢支持~",
                    "time": "2020-10-20 16:00:00",
                    "user": {
                        "userid": "5d3e6b2a000000000101c3f2",
                        "nickname": "护肤小达人"
                    }
                }
            ]
        }
    ],
    "total": 128,
    "hasMore": true
}

4. 笔记关联商品API

接口URL

bash 复制代码
https://www.xiaohongshu.com/api/sns/v1/note/{note_id}/goods

返回JSON示例

json 复制代码
json
{
    "goodsList": [
        {
            "id": "5d3e6b2a000000000101c3f4",
            "name": "某品牌洗面奶",
            "price": "128",
            "image": "https://ci.xiaohongshu.com/5d3e6b2a000000000101c3f4",
            "link": "https://www.xiaohongshu.com/goods/5d3e6b2a000000000101c3f4",
            "tags": ["清洁", "氨基酸"]
        }
    ]
}

Python实现示例

python 复制代码
python
import requests
import json
import time
import hashlib
 
class XiaoHongShuAPI:
    def __init__(self, cookie=None):
        self.session = requests.Session()
        self.headers = {
            "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1",
            "X-Sign": self._generate_sign(),
            "Cookie": cookie
        }
    
    def _generate_sign(self):
        """生成X-Sign签名(简化版,实际需要更复杂的逻辑)"""
        timestamp = str(int(time.time()))
        raw = f"timestamp={timestamp}&key=your_secret_key"
        return hashlib.md5(raw.encode()).hexdigest()
    
    def get_note_detail(self, note_id):
        """获取笔记详情"""
        url = f"https://www.xiaohongshu.com/api/sns/v1/note/{note_id}"
        try:
            response = self.session.get(url, headers=self.headers)
            response.raise_for_status()
            data = response.json()
            
            # 提取关键数据
            note_data = {
                "id": data.get("id"),
                "title": data.get("title"),
                "desc": data.get("desc"),
                "likes": data.get("likes"),
                "comments": data.get("comments"),
                "images": [img["url"] for img in data.get("images", [])],
                "user": {
                    "id": data.get("user", {}).get("userid"),
                    "name": data.get("user", {}).get("nickname"),
                    "avatar": data.get("user", {}).get("image")
                },
                "tags": [tag["name"] for tag in data.get("tags", [])]
            }
            
            return {"success": True, "data": note_data}
        except Exception as e:
            return {"success": False, "error": str(e)}
    
    def get_note_comments(self, note_id, page=1, page_size=20):
        """获取笔记评论"""
        url = f"https://www.xiaohongshu.com/api/sns/v1/note/{note_id}/comments"
        params = {
            "page": page,
            "page_size": page_size
        }
        try:
            response = self.session.get(url, headers=self.headers, params=params)
            response.raise_for_status()
            data = response.json()
            
            comments = []
            for comment in data.get("comments", []):
                comments.append({
                    "id": comment.get("id"),
                    "content": comment.get("content"),
                    "likes": comment.get("likes"),
                    "user": {
                        "id": comment.get("user", {}).get("userid"),
                        "name": comment.get("user", {}).get("nickname")
                    },
                    "replies": [{
                        "id": reply.get("id"),
                        "content": reply.get("content"),
                        "user": {
                            "id": reply.get("user", {}).get("userid"),
                            "name": reply.get("user", {}).get("nickname")
                        }
                    } for reply in comment.get("subComments", [])]
                })
            
            return {
                "success": True,
                "data": {
                    "comments": comments,
                    "total": data.get("total", 0),
                    "has_more": data.get("hasMore", False)
                }
            }
        except Exception as e:
            return {"success": False, "error": str(e)}
 
# 使用示例
if __name__ == "__main__":
    # 需要替换为有效的cookie
    cookie = "your_xiaohongshu_cookie_here"
    api = XiaoHongShuAPI(cookie=cookie)
    
    # 获取笔记详情
    note_id = "5f8d3a3a000000000102e3a1"
    detail = api.get_note_detail(note_id)
    print(json.dumps(detail, ensure_ascii=False, indent=2))
    
    # 获取笔记评论
    comments = api.get_note_comments(note_id)
    print(json.dumps(comments, ensure_ascii=False, indent=2))

注意事项

  1. 签名算法:小红书的X-Sign签名算法是动态的,可能需要逆向分析其APP或Web端的生成逻辑
  2. Cookie:部分接口需要有效的登录Cookie才能访问
  3. 频率限制:建议添加适当的请求间隔,避免被封禁
  4. 数据解析:实际返回的JSON结构可能更复杂,需要根据具体情况调整解析逻辑
  5. 合法性:确保你的数据采集行为符合小红书的用户协议和相关法律法规
  6. 移动端API:小红书的移动端API通常比Web端更稳定,建议优先使用

如需更稳定的解决方案,建议:

  • 使用小红书官方提供的开发者API(如果有权限)
  • 考虑使用Selenium等浏览器自动化工具
  • 加入代理IP池和请求头随机化
  • 实现完善的错误处理和重试机制
相关推荐
RestCloud6 小时前
iPaaS实施的前提是先进行集成关系的梳理
api
RestCloud1 天前
通过ETL工具,同步SQLserver数据至starrocks数据库
api
RestCloud1 天前
爆单不慌!RestCloud iPaaS让618双11财务对账丝滑到飞起
api
wuzuyu3651 天前
SyntaxError: Failed to execute ‘open‘ on ‘XMLHttpRequest‘: Invalid URL
ajax·html·api
老顾聊技术1 天前
网关如何聚合各个微服务的接口文档?
微服务·api
掘金安东尼2 天前
🚀 6 行 HTML,让应用瞬间“起飞”:Speculation Rules API 全解析
前端·api·浏览器
RestCloud2 天前
从易用性的角度来看,哪个ETL平台比较好用?
api
RestCloud2 天前
了解CDC(变更数据捕获)如何革新数据集成方式
api
用户268001379192 天前
京东商品评论API接口系列,json数据返回
api