网淘吧来吧,欢迎您!

返回首页 微信
微信
手机版
手机版

小红书

2026-03-28 新闻来源:网淘吧 围观:11
电脑广告
手机广告

小红书技能

小红书(XiaoHongShu / Little Red Book)数据采集和交互工具包。基于RedCrack纯Python逆向工程实现。

快速开始

安装

依赖项已安装:

pip install aiohttp loguru pycryptodome getuseragent

基本用法

import asyncio
import sys
sys.path.insert(0, r'C:\\Users\\Chocomint\\.openclaw\\workspace\\xiaohongshu\\scripts')

from request.web.xhs_session import create_xhs_session

async def main():
    # ✅ 推荐:不强制代理(有代理再填 proxy)
    # 说明:当前小红书接口经常对“未登录/游客”限制搜索能力。
    # 如果 search 报 code=-104(未登录无权限),请提供 web_session。
    xhs = await create_xhs_session(proxy=None, web_session="YOUR_WEB_SESSION_OR_NONE")

    # Search notes
    res = await xhs.apis.note.search_notes("美妆")
    data = await res.json()
    print(data)

    await xhs.close_session()

asyncio.run(main())

核心功能

1. 搜索与发现

通过关键词搜索笔记:

res = await xhs_session.apis.note.search_notes("口红")

获取首页推荐(热门):

# 注意:get_homefeed 需要 category 参数
res = await xhs_session.apis.note.get_homefeed(
    xhs_session.apis.note.homefeed_category_enum.FOOD
)

获取笔记详情:

# note_detail 需要 note_id + xsec_token(有时在搜索结果 item 里叫 xsec_token)
res = await xhs_session.apis.note.note_detail(note_id, xsec_token)

2. 用户互动

获取用户信息:

res = await xhs_session.apis.auth.get_self_simple_info()

关注用户:

res = await xhs_session.apis.user.follow_user(user_id)

点赞笔记:

res = await xhs_session.apis.note.like_note(note_id)

3. 评论

获取笔记的评论:

# comments 也需要 note_id + xsec_token
res = await xhs_session.apis.comments.get_comments(note_id, xsec_token)

配置

代理

代理不是硬性要求(本技能可以proxy=None运行)。但在以下情况建议使用代理:

  • 网络环境不稳定/请求超时
  • 频繁触发风控(例如 461)想换出口 IP 试试

不使用代理:

xhs = await create_xhs_session(proxy=None, web_session=None)

使用代理:

xhs = await create_xhs_session(
    proxy="http://127.0.0.1:7890",
    web_session="your_web_session_cookie"  # 需要登录能力时再提供
)

加密参数

所有加密参数均为自动生成:

  • Cookies: a1, webId, acw_tc, web_session, sec_poison_id, websectiga, gid
  • 请求头: x-s, x-s-common, x-t, x-b3-traceid, x-xray-traceid

配置文件:scripts/request/web/encrypt/web_encrypt_config.ini

会话管理

访客模式(未登录)

xhs_session = await create_xhs_session(proxy="http://127.0.0.1:7890")

认证模式(已登录)

xhs_session = await create_xhs_session(
    proxy="http://127.0.0.1:7890",
    web_session="030037afxxxxxxxxxxxxxxxxxxxaeb59d5b4"  # Your cookie
)

关闭会话

await xhs_session.close_session()

链接与ID(重要)

  • 小红书笔记的公开标识通常是note_id(十六进制风格字符串),例如:697cc945000000000a02cdad
  • 这个 note_id可以进行数学意义上的 16 进制→10 进制转换,但那只是“另一种表示法”,不会变成小红书的短数字 ID(类似 App Store 的 id741292507),也不会更适合拼接小红书链接。
  • 我们通过接口得到的可直接打开的网页链接通常形如:
    • https://www.xiaohongshu.com/explore/<笔记ID>?xsec_token=...&xsec_source=pc_search
  • xhslink.com 短链一般需要在 App/登录态里通过“分享→复制链接”获得;仅靠当前接口通常拿不到。

输出到聊天时的链接美化(推荐)

为了避免长链接难看,优先用文本标签超链接

  • Markdown:[标题](https://www.xiaohongshu.com/explore/...)

可用 API

所有 API 均通过xhs_session.apis.*访问:

认证 (apis.auth):

  • get_self_simple_info()- 获取当前用户信息

笔记 (apis.note):

  • search_notes(keyword)- 按关键词搜索笔记
  • get_homefeed(category)- 获取首页动态
  • note_detail(note_id, share_token)- 获取笔记详情
  • like_note(note_id)- 点赞笔记

评论 (apis.comments):

  • get_comments(note_id, share_token)- 获取笔记评论

用户 (apis.user):

  • follow_user(user_id)- 关注用户
  • get_user_info(user_id)- 获取用户详情

示例工作流

工作流 1:搜索并提取笔记

async def search_example():
    xhs_session = await create_xhs_session(proxy="http://127.0.0.1:7890")

    # Search for makeup tutorials
    res = await xhs_session.apis.note.search_notes("美妆教程")
    data = await res.json()

    for note in data['data']['items']:
        print(f"Title: {note['display_title']}")
        print(f"Author: {note['user']['nickname']}")
        print(f"Likes: {note['liked_count']}")
        print("---")

    await xhs_session.close_session()

工作流 2:获取评论进行分析

async def comments_example():
    xhs_session = await create_xhs_session(proxy="http://127.0.0.1:7890")

    note_id = "64f1a2d30000000013003689"
    res = await xhs_session.apis.comments.get_comments(note_id, "")
    data = await res.json()

    for comment in data['data']['comments']:
        print(f"User: {comment['user']['nickname']}")
        print(f"Content: {comment['content']}")
        print(f"Likes: {comment['like_count']}")
        print("---")

    await xhs_session.close_session()

工作流 3:用户资料分析

async def profile_example():
    xhs_session = await create_xhs_session(
        proxy="http://127.0.0.1:7890",
        web_session="your_cookie_here"
    )

    # Get self info
    res = await xhs_session.apis.auth.get_self_simple_info()
    data = await res.json()

    print(f"Username: {data['data']['user']['nickname']}")
    print(f"Followers: {data['data']['user']['follows']}")
    print(f"Fans: {data['data']['user']['fans']}")

    await xhs_session.close_session()

重要提示

  1. 需要使用代理由于小红书的反爬措施,大多数操作都需要代理
  2. 速率限制:请合理控制请求频率,避免IP被封禁
  3. 身份验证:部分操作需要登录(web_session cookie)
  4. 法律合规:仅限用于合法的研究和数据分析目的

技术细节

基于RedCrack- 纯Python逆向工程,解析小红书的加密算法。

自动处理的内容:

  • Base64/Base58自定义编码
  • RC4/XOR加密
  • MD5/SHA256哈希
  • 自定义签名生成(x-s, x-s-common)
  • 动态Cookie生成(a1, webId, sec_poison_id等)

无需JavaScript运行时- 所有加密均采用纯Python实现。

故障排除

连接错误

  • 验证您的代理是否在配置的端口上运行
  • 如果需要,尝试不同的代理服务器
  • 检查网络连接

461 错误(风控/安全校验)

  • 这通常不是代码语法问题,而是触发了小红书的风控/安全校验。
  • 典型现象:OtherStatusError: 461 异常,或者接口返回看似 success=true 但 HTTP=461。

应对建议:

  • 降低频率/添加随机延迟、避免并发
  • 更换关键词/更换端点(例如先用搜索获取 note_id + xsec_token,再查询详情)
  • 使用稳定的登录状态(web_session)
  • 必要时更换代理出口

401/403 错误

  • web_session 可能已过期
  • 小红书可能更新了风控参数/签名逻辑(需要更新逆向实现)

导入错误

  • 确保所有依赖项都已安装:pip install aiohttp loguru pycryptodome getuseragent
  • 检查技能路径是否正确sys.path.insert()
免责申明
部分文章来自各大搜索引擎,如有侵权,请与我联系删除。
打赏
文章底部电脑广告
手机广告位-内容正文底部

相关文章

您是本站第280518名访客 今日有104篇新文章/评论