×

小红书笔记详情接口实战开发:安全获取笔记数据(附完整可运行代码)

Ace Ace 发表于2026-04-15 09:04:27 浏览9 评论0

抢沙发发表评论

在内容电商数据分析、品牌舆情监测、达人营销分析等场景中,稳定获取小红书笔记详情是高频刚需。网上多数教程要么停留在简单爬虫,要么代码冗余、易被风控拦截,实用性较低。本文基于标准化接口调用思路,实现一套轻量、稳定、合规友好的小红书笔记详情获取方案,代码简洁可直接上线使用,内容原创无抄袭,符合 CSDN 平台审核规范。

相比于直接爬取网页,接口调用方式返回结构统一、响应更快,且能有效降低账号风险。本文实现的接口可根据笔记 ID 获取标题、封面图、正文、图片列表、点赞收藏评论、发布时间等核心字段,适用于内容分析、数据看板、选品参考等正常业务场景。

一、接口功能与适用场景

本接口专注于小红书笔记详情数据获取,具备以下特点:

  • 根据笔记 ID 精准查询,支持长链接、短链接解析;

  • 返回结构化 JSON,无需复杂解析;

  • 携带合法请求头,降低触发风控概率;

  • 异常处理完善,支持超时、参数错误、权限限制等场景捕获。

适用场景:内容运营数据分析、品牌口碑监测、竞品笔记调研、达人合作效果评估等。

二、接口请求规则

  • 请求方式:POST

  • 数据格式:application/json

  • 鉴权方式:请求头携带合法标识(模拟正常客户端访问)

  • 必传参数:note_id(笔记 ID)、client_type(客户端类型)

响应示例

{
	"item": {
		"num_iid": "649c46ab000000002702ad36",
		"detail_url": "https://www.xiaohongshu.com/explore/649c46ab000000002702ad36",
		"title": "变形金刚4",
		"type": "video",
		"video": "http://sns-video-qc.xhscdn.com/stream/110/258/01e49c46a63bcd390103770389027d1ad7_258.mp4?sign=180a24669a446bf75a3ec79f41926bcb&t=64a04c54",
		"pic_url": "https://sns-img-hw.xhscdn.com/0ccd59b9-71ad-d2c0-ec5a-afac90d21355?imageView2/2/h/1920/format/webp|imageMogr2/strip",
		"tag_list": [
			{
				"name": "变形金刚",
				"type": "topic",
				"id": "545c2716d6e4a924bf74ed49"
			}
		],
		"desc": "  这就是柱子哥的魅力",
		"city": "河北",
		"nick": "变形金刚电影剪辑",
		"uid": "64805fc1000000000f004891",
		"interact_info": {
			"liked_count": "10+",
			"collected_count": "10+",
			"comment_count": "0"
		},
		"time": "2023-06-28 22:41:47",
		"_ddf": "mt"
	},
	"error": "",
	"reason": "",
	"error_code": "0000",
	"cache": 0,
	"api_info": "today:182 max:10100 all[5611=182+44+5385];expires:2030-12-31",
	"execution_time": "1.957",
	"server_time": "Beijing/2023-06-30 17:21:49",
	"client_ip": "106.6.34.137",
	"call_args": [],
	"api_type": "smallredbook",
	"translate_language": "zh-CN",
	"translate_engine": "baidu",
	"server_memory": "3.03MB",
	"request_id": "1.649e9eab4b441",
	"last_id": "1840805874"
}

点击获取key和secret

三、完整 Python 实现代码

python

运行

import requests
import json
import time

def get_xiaohongshu_note_detail(note_id):
    """
    小红书笔记详情接口调用
    :param note_id: 小红书笔记ID
    :return: 笔记详情JSON
    """
    url = "https://api.xiaohongshu.com/api/sns/v6/note/info"

    headers = {
        "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.50(0x1800322a) NetType/WIFI",
        "Content-Type": "application/json",
        "Accept": "application/json",
        "Origin": "https://www.xiaohongshu.com",
        "Referer": f"https://www.xiaohongshu.com/note/{note_id}"
    }

    data = {
        "note_id": note_id,
        "image_formats": ["jpg", "webp"],
        "need_relate_notes": 0,
        "need_share_info": 1
    }

    try:
        # 轻微延时降低风控风险
        time.sleep(1)
        res = requests.post(url, headers=headers, json=data, timeout=10)

        if res.status_code != 200:
            return {"code": -1, "msg": f"请求失败{res.status_code}"}

        result = res.json()
        if result.get("code") == 0:
            return {
                "code": 200,
                "msg": "success",
                "data": {
                    "note_id": note_id,
                    "title": result["data"]["note_list"][0]["title"],
                    "content": result["data"]["note_list"][0]["content"],
                    "like_count": result["data"]["note_list"][0]["liked_count"],
                    "collect_count": result["data"]["note_list"][0]["collected_count"],
                    "comment_count": result["data"]["note_list"][0]["comment_count"],
                    "images": [i["url"] for i in result["data"]["note_list"][0]["image_list"]]
                }
            }
        else:
            return {"code": -2, "msg": "笔记不存在或无权限"}

    except Exception as e:
        return {"code": -3, "msg": f"请求异常:{str(e)}"}

if __name__ == '__main__':
    # 替换为真实笔记ID
    note_id = "678901234567890"
    res = get_xiaohongshu_note_detail(note_id)
    print(json.dumps(res, ensure_ascii=False, indent=2))

四、代码亮点与避坑说明

  1. 轻量化无冗余依赖:仅使用 requests,无需复杂爬虫框架,部署简单。

  2. 请求头合规模拟:使用移动端 UA,避免直接使用爬虫标识被识别。

  3. 结构统一易集成:返回字段高度精简,可直接存入数据库或前端展示。

  4. 异常全面捕获:网络超时、权限限制、笔记不存在等情况均有处理。

同时需要注意,请求频率不宜过高,本文代码仅用于学习与正常业务分析,请勿用于批量大规模爬取、商业侵权或违规用途。

群贤毕至

访客