A CLI for Xiaohongshu (小红书) — search, read notes, view profiles
Project description
xhs-cli
中文 | English
小红书命令行工具 — 在终端中搜索笔记、查看主页、点赞、收藏、评论。
推荐项目
- twitter-cli - 在终端中操作 X/Twitter 的 CLI 工具
- bilibili-cli - 哔哩哔哩 CLI 工具
功能
- 搜索 — 按关键词搜索笔记,Rich 表格展示
- 阅读 — 查看笔记内容、数据、评论
- 用户资料 — 查看用户信息、笔记、粉丝、关注
- 推荐 Feed — 获取探索页推荐内容
- 话题 — 搜索话题标签
- 互动 — 点赞/取消、收藏/取消、评论、删除笔记
- 发布 — 发布图文笔记
- 认证 — 自动提取 Chrome cookie,或扫码登录
- JSON 输出 — 所有数据命令支持
--json - Token 自动缓存 —
xsec_token搜索后自动缓存,后续命令免手动传
命令一览
| 分类 | 命令 | 说明 |
|---|---|---|
| Auth | login, logout, status, whoami |
登录、退出、状态检查、查看个人资料 |
| Read | search, read, feed, topics |
搜索笔记、阅读详情、推荐 Feed、搜索话题 |
| Users | user, user-posts, followers, following |
查看资料、列出笔记/粉丝/关注 |
| Engage | like, unlike, comment, delete |
点赞、取消点赞、评论、删除笔记 |
| Favorites | favorite, unfavorite, favorites |
收藏、取消收藏、查看收藏列表 |
| Post | post |
发布图文笔记 |
所有数据命令支持
--json输出。xsec_token自动缓存,无需手动传递。
安装
需要 Python 3.8+。
# 推荐:使用 uv
uv tool install xhs-cli
# 或使用 pipx
pipx install xhs-cli
从源码安装(开发用)
git clone git@github.com:jackwener/xhs-cli.git
cd xhs-cli
uv sync
本地一键冒烟测试
在本地已登录(有 ~/.xhs-cli/cookies.json)的情况下,直接运行:
./scripts/smoke_local.sh
可选地传递 pytest 参数(例如只跑某个用例):
./scripts/smoke_local.sh -k whoami
默认只跑无副作用命令(integration and not live_mutation)。如需额外验证
like/favorite/comment/post/delete,显式开启:
XHS_SMOKE_MUTATION=1 ./scripts/smoke_local.sh
可选环境变量:
XHS_SMOKE_COMMENT_TEXT="smoke test comment"
XHS_SMOKE_POST_IMAGES="/abs/a.jpg,/abs/b.jpg"
XHS_SMOKE_POST_TITLE="smoke title"
XHS_SMOKE_POST_CONTENT="smoke content"
使用
登录
# 自动从 Chrome 提取 cookie(推荐)
xhs login
# 手动提供 cookie 字符串
xhs login --cookie "a1=xxx; web_session=yyy"
# 快速检查已保存的登录状态(不启动浏览器,不读取浏览器 cookie)
xhs status
# 查看个人资料
xhs whoami
xhs whoami --json
# 退出登录
xhs logout
搜索
xhs search "咖啡"
xhs search "咖啡" --json
阅读笔记
# 查看笔记(xsec_token 从缓存自动解析)
xhs read <note_id>
# 包含评论
xhs read <note_id> --comments
# 手动指定 xsec_token
xhs read <note_id> --xsec-token <token>
用户
# 查看用户资料(使用内部 user_id,非小红书号)
xhs user <user_id>
# 列出用户笔记
xhs user-posts <user_id>
# 粉丝 / 关注
xhs followers <user_id>
xhs following <user_id>
推荐 & 话题
xhs feed
xhs topics "旅行"
互动
# 点赞 / 取消(xsec_token 自动解析)
xhs like <note_id>
xhs like <note_id> --undo
# 收藏 / 取消
xhs favorite <note_id>
xhs favorite <note_id> --undo
# 评论
xhs comment <note_id> "好棒!"
# 删除自己的笔记
xhs delete <note_id>
# 查看收藏列表
xhs favorites
xhs favorites --max 10
发布笔记
xhs post "标题" --image photo1.jpg --image photo2.jpg --content "正文内容"
xhs post "标题" --image photo1.jpg --content "正文内容" --json
其他
xhs --version
xhs -v search "咖啡" # 调试日志
xhs --help
架构
CLI (click) → XhsClient (camoufox 浏览器)
↓ 导航到真实页面
window.__INITIAL_STATE__ → 提取结构化数据
使用 camoufox(反指纹 Firefox)像真实用户一样浏览小红书。数据从页面的 window.__INITIAL_STATE__ 中提取,与正常浏览完全一致。
工作原理
- 认证 — 优先读取
~/.xhs-cli/cookies.json;未命中时通过 browser-cookie3 从本地 Chrome 提取 cookie,失败则 fallback 到扫码登录。 - 浏览 — 使用 camoufox 导航到真实页面,所有流量与正常用户浏览一致。
- 数据提取 — 从
window.__INITIAL_STATE__提取结构化数据。 - Token 缓存 — 搜索/Feed 后
xsec_token自动缓存到~/.xhs-cli/token_cache.json。 - 互动操作 — 点赞、收藏、评论通过点击真实 DOM 按钮实现。
作为 AI Agent Skill 使用
xhs-cli 自带 SKILL.md,让 AI Agent 能自动学习并使用本工具。
Claude Code / Antigravity
# 克隆到项目的 skills 目录
mkdir -p .agents/skills
git clone git@github.com:jackwener/xhs-cli.git .agents/skills/xhs-cli
# 或者只复制 SKILL.md
curl -o .agents/skills/xhs-cli/SKILL.md \
https://raw.githubusercontent.com/jackwener/xhs-cli/main/SKILL.md
添加后,支持 .agents/skills/ 的 AI Agent 会自动发现并使用 xhs-cli 命令。
OpenClaw / ClawHub
官方支持 OpenClaw 和 ClawHub 生态。通过 ClawHub 安装:
clawhub install xiaohongshu-cli
安装后即可在 OpenClaw 中直接使用所有 xhs-cli 命令。
注意事项
- Cookie 存储在
~/.xhs-cli/cookies.json,权限0600。 xhs status只检查本地已保存 cookie,不会触发浏览器 cookie 提取。xhs post可能要求额外登录创作平台(https://creator.xiaohongshu.com)。- 使用 headless Firefox,不会弹出浏览器窗口。
- 首次运行需下载 camoufox 浏览器(
python -m camoufox fetch)。 - 用户资料查询需要内部 user_id(十六进制),不是小红书号。
License
Apache License 2.0
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file xhs_cli-0.1.1.tar.gz.
File metadata
- Download URL: xhs_cli-0.1.1.tar.gz
- Upload date:
- Size: 323.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d170dd660b6947ccfc71cc6752c8db03afa21b9d631ad2735f9b64b79ccb8543
|
|
| MD5 |
37899af603f08d160d1287d41e9b59c7
|
|
| BLAKE2b-256 |
d5f4b544330c5a3bdfc7f10912e2844cac627d85848336fd23dde042004f879c
|
Provenance
The following attestation bundles were made for xhs_cli-0.1.1.tar.gz:
Publisher:
publish.yml on jackwener/xhs-cli
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
xhs_cli-0.1.1.tar.gz -
Subject digest:
d170dd660b6947ccfc71cc6752c8db03afa21b9d631ad2735f9b64b79ccb8543 - Sigstore transparency entry: 1042752541
- Sigstore integration time:
-
Permalink:
jackwener/xhs-cli@b86cde6a8b980f271e3546746a86e8b51aee464a -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/jackwener
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b86cde6a8b980f271e3546746a86e8b51aee464a -
Trigger Event:
push
-
Statement type:
File details
Details for the file xhs_cli-0.1.1-py3-none-any.whl.
File metadata
- Download URL: xhs_cli-0.1.1-py3-none-any.whl
- Upload date:
- Size: 34.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f306d4da350b5660e750521b6bbf23517e8b26bd81ef6bb3ebc3c308d184a2a1
|
|
| MD5 |
39a49403787e5013e4c8b4921b3714d3
|
|
| BLAKE2b-256 |
9dd0f205bb08b45b259fc3642578b22dccd30d2b408a47ef1c2243bf4f753e4c
|
Provenance
The following attestation bundles were made for xhs_cli-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on jackwener/xhs-cli
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
xhs_cli-0.1.1-py3-none-any.whl -
Subject digest:
f306d4da350b5660e750521b6bbf23517e8b26bd81ef6bb3ebc3c308d184a2a1 - Sigstore transparency entry: 1042752559
- Sigstore integration time:
-
Permalink:
jackwener/xhs-cli@b86cde6a8b980f271e3546746a86e8b51aee464a -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/jackwener
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b86cde6a8b980f271e3546746a86e8b51aee464a -
Trigger Event:
push
-
Statement type: