Skip to main content

小红书 MCP 工具 - 支持登录、发布图文/视频、点赞评论等功能

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

xhs-mcp-py

Python License

小红书 MCP 工具的 Python 实现,基于 Playwright 浏览器自动化,支持登录、发布、搜索、点赞、收藏、评论等功能。

特性

  • 🔐 扫码登录 - 支持二维码扫码登录,自动保存 cookies
  • 📝 发布内容 - 支持发布图文、视频,支持定时发布
  • 🔍 搜索内容 - 支持关键词搜索,支持筛选条件
  • ❤️ 互动功能 - 支持点赞、收藏、评论
  • 📊 数据获取 - 获取首页推荐、笔记详情、用户主页
  • 🤖 MCP 协议 - 支持 MCP 协议,可与 AI 助手集成

安装

pip install xhs-mcp-py

# 首次运行需安装浏览器
playwright install chromium

快速开始

命令行使用

# 扫码登录(会打开浏览器窗口)
xhs-mcp login --no-headless

# 检查登录状态
xhs-mcp status

# 发布图文
xhs-mcp publish -t "标题" -c "正文内容" -i image1.jpg -i image2.jpg --tag 旅行 --tag 美食

# 发布视频
xhs-mcp publish-video -t "标题" -c "正文内容" -v video.mp4

# 搜索内容
xhs-mcp search -k "关键词"

# 退出登录
xhs-mcp logout

# 启动 MCP 服务
xhs-mcp serve

Python API

import asyncio
from xhs_mcp import XhsClient

async def main():
    # 创建客户端(headless=False 可看到浏览器窗口)
    async with XhsClient(headless=False) as client:
        # 扫码登录
        await client.login()
        
        # 检查登录状态
        if await client.is_logged_in():
            # 发布图文
            result = await client.publish(
                title="我的标题",
                content="正文内容",
                images=["./img1.jpg", "./img2.jpg"],
                tags=["旅行", "美食"]
            )
            print(f"发布结果: {result.status}")
            
            # 搜索内容
            feeds = await client.search("Python")
            for feed in feeds.feeds[:5]:
                print(f"- {feed.display_title}")
            
            # 点赞
            await client.like(feed_id="xxx", xsec_token="xxx")
            
            # 收藏
            await client.favorite(feed_id="xxx", xsec_token="xxx")
            
            # 评论
            await client.comment(feed_id="xxx", xsec_token="xxx", content="好文章!")

asyncio.run(main())

MCP 工具列表

工具名 说明
check_login_status 检查登录状态
get_login_qrcode 获取登录二维码
delete_cookies 删除 cookies,重置登录
publish_content 发布图文内容
publish_with_video 发布视频内容
list_feeds 获取首页推荐列表
search_feeds 搜索内容
get_feed_detail 获取笔记详情
get_user_profile 获取用户主页
like_feed 点赞/取消点赞
favorite_feed 收藏/取消收藏
post_comment 发表评论

MCP 配置示例

在 Claude Desktop 或其他 MCP 客户端中配置:

{
  "mcpServers": {
    "xiaohongshu": {
      "command": "xhs-mcp",
      "args": ["serve"]
    }
  }
}

API 参考

XhsClient

方法 说明
login() 交互式扫码登录
is_logged_in() 检查是否已登录
delete_cookies() 删除 cookies
publish(title, content, images, tags) 发布图文
publish_video(title, content, video, tags) 发布视频
search(keyword, filters) 搜索内容
get_feeds() 获取首页推荐
get_feed_detail(feed_id, xsec_token) 获取笔记详情
get_user_profile(user_id, xsec_token) 获取用户主页
like(feed_id, xsec_token) 点赞
unlike(feed_id, xsec_token) 取消点赞
favorite(feed_id, xsec_token) 收藏
unfavorite(feed_id, xsec_token) 取消收藏
comment(feed_id, xsec_token, content) 发表评论

开发

# 克隆项目
git clone https://github.com/luweizheng/xhs-mcp-py.git
cd xhs-mcp-py

# 安装开发依赖
pip install -e ".[dev]"

# 安装浏览器
playwright install chromium

# 运行测试
pytest

注意事项

  • 首次使用需要扫码登录,登录后 cookies 会保存到本地
  • 建议使用 --no-headless 模式进行登录,以便扫码
  • cookies 默认保存在当前目录的 cookies.json 文件
  • 可通过环境变量 COOKIES_PATH 指定 cookies 文件路径

License

MIT

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

xhs_mcp_py-0.1.0.tar.gz (19.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xhs_mcp_py-0.1.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file xhs_mcp_py-0.1.0.tar.gz.

File metadata

  • Download URL: xhs_mcp_py-0.1.0.tar.gz
  • Upload date:
  • Size: 19.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for xhs_mcp_py-0.1.0.tar.gz
Algorithm Hash digest
SHA256 defaaed39e405451ff5db2d3a2a41f016f6ed48db376963cbbd197927bdb8a02
MD5 e22dd24f9b1afb558341d3319af6d60e
BLAKE2b-256 530c199399b412e9c9aff09362543828ee69efddf4d795c34ec46690239b17c7

See more details on using hashes here.

Provenance

The following attestation bundles were made for xhs_mcp_py-0.1.0.tar.gz:

Publisher: publish.yml on luweizheng/xhs-mcp-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file xhs_mcp_py-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: xhs_mcp_py-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for xhs_mcp_py-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bb9a13eaa62ecbe0532eadc8a535a2b552eb06b140386b554346c3eea38c850a
MD5 3096e21a4bef622cbd934d8cc73a36cd
BLAKE2b-256 4c44e992ffcfacd6714c7bc6f91f5771a7a69aacecf8bee3def8d1fc3807fede

See more details on using hashes here.

Provenance

The following attestation bundles were made for xhs_mcp_py-0.1.0-py3-none-any.whl:

Publisher: publish.yml on luweizheng/xhs-mcp-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page