xhs-cli

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

小红书 CLI

Xiaohongshu (XHS) CLI

通过 Playwright 浏览器自动化实现小红书 Web API 调用,无需官方 API Key。
Realize Xiaohongshu Web API calls via Playwright browser automation, no official API Key required.

前置条件

Prerequisites

  1. 安装依赖:
    pip install -r requirements.txt && playwright install chromium
  2. 首次使用需登录:
    python xhs.py login
    (会打开浏览器,手动扫码)
验证登录状态:
bash
python skills/xhs-cli/xhs.py me
  1. Install dependencies:
    pip install -r requirements.txt && playwright install chromium
  2. Log in for the first use:
    python xhs.py login
    (a browser will open, scan the QR code manually)
Verify login status:
bash
python skills/xhs-cli/xhs.py me

触发条件

Trigger Conditions

以下情况使用此 Skill:
  • 用户要搜索小红书内容
  • 用户要查看某篇笔记的详情或评论
  • 用户要了解某个小红书用户的主页
  • 用户要查看自己的账号数据
  • 用户要发布图文笔记
Use this Skill in the following scenarios:
  • Users want to search Xiaohongshu content
  • Users want to view details or comments of a note
  • Users want to learn about a Xiaohongshu user's homepage
  • Users want to check their own account data
  • Users want to publish image-text notes

搜索笔记

Search Notes

bash
python skills/xhs-cli/xhs.py search "关键词"
python skills/xhs-cli/xhs.py search "关键词" --sort time     # 按时间排序
python skills/xhs-cli/xhs.py search "关键词" --type video    # 只搜视频
bash
python skills/xhs-cli/xhs.py search "keywords"
python skills/xhs-cli/xhs.py search "keywords" --sort time     # Sort by time
python skills/xhs-cli/xhs.py search "keywords" --type video    # Search only videos

笔记详情和评论

Note Details and Comments

bash
python skills/xhs-cli/xhs.py detail <note_id> <xsec_token>    # 笔记详情
python skills/xhs-cli/xhs.py comments <note_id> <xsec_token>  # 评论列表
python skills/xhs-cli/xhs.py note <note_id> <xsec_token>      # 详情 + 评论
note_id 和 xsec_token 从搜索结果中获取。
bash
python skills/xhs-cli/xhs.py detail <note_id> <xsec_token>    # Note details
python skills/xhs-cli/xhs.py comments <note_id> <xsec_token>  # Comment list
python skills/xhs-cli/xhs.py note <note_id> <xsec_token>      # Details + comments
note_id and xsec_token are obtained from search results.

用户信息

User Information

bash
python skills/xhs-cli/xhs.py me                     # 当前登录用户
python skills/xhs-cli/xhs.py user <user_id>          # 查看用户主页
python skills/xhs-cli/xhs.py unread                  # 未读通知
bash
python skills/xhs-cli/xhs.py me                     # Currently logged-in user
python skills/xhs-cli/xhs.py user <user_id>          # View user homepage
python skills/xhs-cli/xhs.py unread                  # Unread notifications

账号数据

Account Data

bash
python skills/xhs-cli/xhs.py stats                   # 所有帖子数据概览
python skills/xhs-cli/xhs.py stats --detail           # 含每篇帖子详情
需要在 config.yaml 中配置
account_name
search_keyword
bash
python skills/xhs-cli/xhs.py stats                   # Overview of all post data
python skills/xhs-cli/xhs.py stats --detail           # Include details of each post
Need to configure
account_name
and
search_keyword
in config.yaml.

发布笔记

Publish Notes

bash
python skills/xhs-cli/xhs_publish.py login                                      # 创作者中心登录
python skills/xhs-cli/xhs_publish.py publish --draft draft.md --images a.jpg b.jpg  # 发布
python skills/xhs-cli/xhs_publish.py publish --draft draft.md --images a.jpg --auto # 跳过确认
草稿文件格式:
markdown
undefined
bash
python skills/xhs-cli/xhs_publish.py login                                      # Creator Center login
python skills/xhs-cli/xhs_publish.py publish --draft draft.md --images a.jpg b.jpg  # Publish
python skills/xhs-cli/xhs_publish.py publish --draft draft.md --images a.jpg --auto # Skip confirmation
Draft file format:
markdown
undefined

标题

Title

帖子标题
Post title

正文

Body

正文内容...
Body content...

话题标签

Hashtags

#标签1 #标签2 #标签3
undefined
#tag1 #tag2 #tag3
undefined

典型工作流

Typical Workflows

「帮我搜一下小红书上关于 XX 的内容」

"Help me search for content about XX on Xiaohongshu"

  1. search "XX"
    搜索
  2. 对感兴趣的笔记
    note <id> <token>
    查看详情+评论
  3. 总结返回
  1. search "XX"
    to search
  2. Use
    note <id> <token>
    for interesting notes to view details + comments
  3. Summarize and return

「看看我的账号数据怎么样」

"How is my account data?"

  1. stats --detail
    获取全部帖子数据
  2. 分析趋势,给出建议
  1. stats --detail
    to get all post data
  2. Analyze trends and give suggestions

「帮我分析一下这个竞品账号」

"Help me analyze this competitor account"

  1. user <user_id>
    获取主页信息
  2. 搜索该用户的帖子
  3. 对爆款帖
    comments
    看评论区反馈
  1. user <user_id>
    to get homepage information
  2. Search for the user's posts
  3. Use
    comments
    on popular posts to check comment feedback

注意事项

Notes

  • 首次使用必须
    login
    扫码登录,Cookie 保存在
    data/xhs_cookie.txt
  • 小红书有反爬机制,高频请求可能触发验证码,建议操作间留间隔
  • 搜索结果中的
    xsec_token
    有时效性,过期需重新搜索
  • 新发的帖子可能数小时后才出现在搜索结果中
  • 发布功能需要额外登录创作者中心:
    xhs_publish.py login
  • Must
    login
    and scan the QR code for the first use, cookies are saved in
    data/xhs_cookie.txt
  • Xiaohongshu has anti-crawling mechanisms, high-frequency requests may trigger verification codes, it is recommended to leave intervals between operations
  • The
    xsec_token
    in search results has a validity period, re-search if it expires
  • Newly posted notes may appear in search results after a few hours
  • The publishing function requires additional login to the Creator Center:
    xhs_publish.py login