mcpField
X
xhs-cli

xhs-cli

github.com/jackwener营销与增长674104 次安装3 周前更新
代码仓库

关于 xhs-cli

xhs-cli 是用于通过命令行操作小红书的工具,适合需要检索内容或管理账号互动的用户。它支持搜索、查看笔记和用户资料、点赞收藏及发布内容,并提供 JSON 输出。

主要功能

  1. 01搜索笔记与话题
  2. 02查看笔记及评论
  3. 03查询用户资料和笔记
  4. 04点赞、收藏与评论
  5. 05发布图文内容
  6. 06支持 JSON 输出

适用场景

  1. 01检索小红书上的主题内容
  2. 02查看用户资料及已发布笔记
  3. 03通过命令行管理点赞和收藏
  4. 04发布带图片的笔记

MCP 相关

输入未说明 MCP 集成或协议支持。

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/jackwener/xhs-cli --skill xhs-cli

安装后在 Claude Code 中输入/xhs-cli确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

README.md
pyproject.toml
.gitignore
README_EN.md
CHANGELOG.md
LICENSE
.github/workflows/
publish.yml
ci.yml
scripts/
smoke_local.sh
tests/
conftest.py
test_auth.py
test_cli.py
test_client.py
test_integration.py
xhs_cli/
__init__.py
auth.py
exceptions.py
cli.py
client.py

详细介绍

说明

另有一个替代包 xiaohongshu-cli,使用逆向工程的 API,运行速度更快。 本包(xhs-cli)采用无头浏览器(camoufox)方案,速度较慢,但更能抵御风控检测。 请根据自身需求选择。

xhs-cli 技能

用于与小红书(小红书)交互的命令行工具。可用于搜索笔记、阅读详情、浏览用户主页,以及执行点赞、收藏和评论等互动操作。

前置条件

# Install (requires Python 3.8+)
uv tool install xhs-cli
# Or: pipx install xhs-cli

身份验证

所有命令都需要有效的 Cookie 才能运行。

xhs status                     # Check saved login session (no browser extraction)
xhs login                      # Auto-extract Chrome cookies
xhs login --cookie "a1=..."    # Or provide cookies manually

身份验证会优先使用已保存的本地 Cookie。如果不可用,则会通过 browser-cookie3 自动检测本地 Chrome Cookie。如果提取失败,可通过二维码登录。

命令参考

搜索
xhs search "咖啡"              # Search notes (rich table output)
xhs search "咖啡" --json       # Raw JSON output
阅读笔记
# View note (xsec_token auto-resolved from search cache)
xhs read <note_id>
xhs read <note_id> --comments  # Include comments
xhs read <note_id> --xsec-token <token>  # Manual token
xhs read <note_id> --json
用户
# Look up user profile (by internal user_id, hex format)
xhs user <user_id>
xhs user <user_id> --json

# List user's published notes
xhs user-posts <user_id>
xhs user-posts <user_id> --json

# Followers / Following
xhs followers <user_id>
xhs following <user_id>
发现
xhs feed                       # Explore page recommended feed
xhs feed --json
xhs topics "旅行"              # Search topics/hashtags
xhs topics "旅行" --json
互动(需要登录)
# Like / Unlike (xsec_token auto-resolved)
xhs like <note_id>
xhs like <note_id> --undo

# Favorite / Unfavorite
xhs favorite <note_id>
xhs favorite <note_id> --undo

# Comment
xhs comment <note_id> "好棒!"

# Delete your own note
xhs delete <note_id>
收藏
xhs favorites                  # List your favorites
xhs favorites --max 10         # Limit count
xhs favorites --json
发布
xhs post "标题" --image photo1.jpg --image photo2.jpg --content "正文"
xhs post "标题" --image photo1.jpg --content "正文" --json
账号
xhs status                     # Quick saved-session check
xhs whoami                     # Full profile info
xhs whoami --json
xhs login                      # Login
xhs logout                     # Clear cookies

JSON 输出

主要查询命令支持 --json,用于输出机器可读的数据:

xhs search "咖啡" --json | jq '.[0].id'           # First note ID
xhs whoami --json | jq '.userInfo.userId'          # Your user ID
xhs favorites --json | jq '.[0].displayTitle'      # First favorite title

AI Agent 常见模式

# Get your user ID for further queries
xhs whoami --json | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('userInfo',{}).get('userId',''))"

# Search and get note IDs (xsec_token auto-cached for later use)
xhs search "topic" --json | python3 -c "import sys,json; [print(n['id']) for n in json.load(sys.stdin)[:3]]"

# Check login before performing actions
xhs status && xhs like <note_id>

# Read a note with comments for summarization
xhs read <note_id> --comments --json

错误处理

  • 命令成功时以代码 0 退出,失败时以非零代码退出
  • 错误消息以 ❌ 开头
  • 需要登录的命令会明确提示运行 xhs login
  • xsec_token 会自动从缓存中解析;也可使用 --xsec-token 作为备用方案

安全注意事项

  • 不要要求用户在聊天记录中分享原始 Cookie 值。
  • 优先通过 xhs login 自动提取,而不是手动输入 Cookie。
  • 如果身份验证失败,请让用户通过 xhs login 重新登录。

评论

支持 Markdown