一、接口调用前提
- 注册与认证 需注册小红书开放平台/万邦开放平台账号,创建应用并获取access_token(授权令牌)。 部分操作需通过企业认证并申请高级权限。
- 接口限制 调用频率受限(如每分钟100次),超限需申请提额。 返回数据可能受用户隐私设置影响(如作者关闭公开权限则无法获取)。
二、接口调用方式
请求地址
bash
GET https://api.xiaohongshu.com/v1/notes/{note_id}
请求头
json
{
"Authorization": "Bearer {access_token}",
"Content-Type": "application/json"
}
必填参数
参数名 | 类型 | 说明 |
---|---|---|
note_id | String | 笔记唯一ID(如649c46ab000000002702ad36 ) |
access_token | String | 开放平台获取的授权令牌 |
可选参数
参数名 | 类型 | 说明 |
---|---|---|
fields | String | 指定返回字段(如title,content,images ) |
timestamp | Int | 请求时间戳(防重放攻击) |
sign | String | 加密签名(需按官方算法生成) |
三、返回数据结构(JSON)
json
{
"code": 200,
"message": "success",
"data": {
"note_id": "649c46ab000000002702ad36",
"title": "春季美妆新品推荐",
"content": "<p>今天分享几款超火的美妆新品...</p>",
"images": [
"https://example.com/image1.jpg",
"https://example.com/image2.jpg"
],
"videos": [
{
"type": "video",
"url": "https://example.com/video.mp4",
"cover": "https://example.com/cover.jpg"
}
],
"tags": ["美妆", "新品"],
"like_count": 1024,
"comment_count": 256,
"favorite_count": 512,
"share_count": 128,
"author": {
"user_id": "987654321",
"nickname": "美妆达人",
"avatar": "https://example.com/avatar.jpg",
"follower_count": 100000
},
"publish_time": "2025-04-25T14:30:00Z",
"location": {
"name": "上海",
"latitude": 31.2304,
"longitude": 121.4737
}
}
}
四、错误处理
常见错误码
错误码 | 说明 | 解决方案 |
---|---|---|
401 | 无效的access_token | 检查令牌是否过期或权限不足 |
404 | 笔记不存在或已删除 | 确认note_id 正确性 |
429 | 调用频率超限 | 优化代码减少请求,或申请提额 |
500 | 服务器内部错误 | 稍后重试并联系技术支持 |
排查步骤
- 检查
access_token
有效性及权限范围。 - 确认
note_id
格式正确(通常为24位字母数字组合)。 - 使用Postman等工具测试接口连通性。
- 查看返回的
message
字段获取具体错误原因。
五、调用示例(Python)
python
import requests
def get_note_details(note_id, access_token):
url = f"https://api.xiaohongshu.com/v1/notes/{note_id}"
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json"
}
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
data = response.json()
if data.get("code") == 200:
return data["data"]
else:
print(f"API错误: {data.get('message')}")
except requests.exceptions.RequestException as e:
print(f"请求失败: {e}")
# 使用示例
note_id = "649c46ab000000002702ad36"
access_token = "YOUR_ACCESS_TOKEN"
details = get_note_details(note_id, access_token)
if details:
print(f"标题: {details['title']}")
print(f"点赞数: {details['like_count']}")
print(f"作者: {details['author']['nickname']}")
六、注意事项
- 合规性 禁止爬取用户隐私数据(如手机号、地址)。 需遵守《小红书开放平台协议》,禁止商业化滥用数据。
- 数据解析 content字段可能包含HTML标签,需用BeautifulSoup等库解析。 图片URL需处理防盗链(部分链接需添加Referer头)。
- 扩展功能 结合搜索API可实现批量获取笔记(需分页参数page和size)。 通过tags字段可构建内容分类标签体系。
如需更高级功能(如评论抓取),需联系小红书商务团队申请特殊权限。