🚀 小红书MCP工具使用指南
本工具借助Playwright框架,能有效模拟小红书平台的用户行为,支持内容发布、数据采集及互动操作,可显著提升运营效率。
🚀 快速开始
环境准备
python -m venv env
source env/bin/activate
安装依赖
pip install -r requirements.txt
登录流程
手动登录
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
page.goto('https://www.xiaohongshu.com')
page.click('#login-btn')
page.wait_for_timeout(5000)
自动化登录(配置cookie)
COOKIES_PATH = 'cookies.json'
context.add_cookies(json.loads(open(COOKIE_PATH).read()))
page.goto('https://www.xiaohongshu.com')
核心功能实现
搜索功能
def search_note(content):
page.type('#search-input', content)
page.press('#search-input', 'Enter')
page.wait_for_selector('.note-list')
return page.querySelectorAll('.note-item')
数据采集
def get_note_info(note_element):
title = note_element.query_selector('.title').text_content()
author = note_element.query_selector('.author').text_content()
content = note_element.query_selector('.content').text_content()
likes = note_element.query_selector('.likes-count').text_content()
return {
'标题': title,
'作者': author,
'内容': content,
'点赞数': likes
}
✨ 主要特性
核心功能
- 用户行为模拟:通过Playwright框架实现小红书平台的用户操作,包括注册、登录、浏览、点赞等。
- 数据采集:支持采集笔记信息、用户信息、评论内容等数据,并存储到数据库中。
- 任务自动化:可配置执行多种运营任务,如自动发布内容、互动(点赞、评论)、关注与粉丝管理。
技术架构
└─ playwright
└─ browser
├─ page
└─ context
└─ selectors
📦 安装指南
环境准备
python -m venv env
source env/bin/activate
安装依赖
pip install -r requirements.txt
💻 使用示例
基础用法
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
page.goto('https://www.xiaohongshu.com')
page.click('#login-btn')
page.wait_for_timeout(5000)
高级用法
import sqlite3
def get_note_info(note_element):
title = note_element.query_selector('.title').text_content()
author = note_element.query_selector('.author').text_content()
content = note_element.query_selector('.content').text_content()
likes = note_element.query_selector('.likes-count').text_content()
return {
'标题': title,
'作者': author,
'内容': content,
'点赞数': likes
}
def save_note(note_info):
conn = sqlite3.connect('notes.db')
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS notes
(id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
author TEXT,
content TEXT,
likes INTEGER)''')
cursor.execute('INSERT INTO notes VALUES (?, ?, ?, ?)',
(note_info['标题'], note_info['作者'],
note_info['内容'], note_info['点赞数']))
conn.commit()
📚 详细文档
代码结构
- xiaohongshu_mcp.py:实现主要功能的核心文件,包含登录、搜索、获取内容和评论、发布评论等功能的代码逻辑。
- requirements.txt:记录项目所需的依赖库。
详细使用说明
登录模块
手动登录
playwright = sync_playwright()
browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
page.goto('https://www.xiaohongshu.com/login')
page.type('#phone-input', '13812345678')
page.click('#sms-login-btn')
page.wait_for_timeout(3000)
verification_code = input('请输入收到的验证码:')
page.type('#code-input', verification_code)
page.click('#submit-btn')
自动化登录
context.add_cookies(json.loads(open(COOKIES_PATH).read()))
page.goto('https://www.xiaohongshu.com')
搜索模块
def search_notes(keyword):
page.type('#search-input', keyword)
page.press('#search-input', 'Enter')
page.wait_for_selector('.note-list')
return page.querySelectorAll('.note-item')
采集模块
def extract_note_info(note_element):
title = note_element.query_selector('h1').text_content()
author = note_element.query_selector('.author-name').text_content()
content = note_element.query_selector('.content').text_content()
likes_count = note_element.query_selector('.likes-count').text_content()
comments_count = note_element.query_selector('.comments-count').text_content()
return {
'标题': title,
'作者': author,
'内容': content,
'点赞数': likes_count,
'评论数': comments_count
}
发布模块
def post_note(content):
page.goto('https://www.xiaohongshu.com/create/note')
page.type('#content-input', content)
page.click('#submit-btn')
page.wait_for_timeout(2000)
🔧 技术细节
常见问题及解决方案
登录失败
原因
解决方案
- 更新Playwright版本:
pip install --upgrade playwright
- 使用最新验证码处理方法:
page.wait_for_selector('#captcha-input')
网页元素找不到
原因
解决方案
- 检查开发者工具,更新选择器路径。
- 使用更稳定的选择器(如
data-testid)代替普通CSS选择器。
性能问题
原因
解决方案
- 合理设置等待时间:
page.wait_for_timeout(2000)
- 使用
playwright dev进行调试,优化脚本执行路径。
高级功能
数据存储
import sqlite3
def save_note(note_info):
conn = sqlite3.connect('notes.db')
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS notes
(id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
author TEXT,
content TEXT,
likes INTEGER)''')
cursor.execute('INSERT INTO notes VALUES (?, ?, ?, ?)',
(note_info['标题'], note_info['作者'],
note_info['内容'], note_info['点赞数']))
conn.commit()
日志记录
import logging
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')
def log_activity(action):
logging.info(f'执行操作:{action}')
📄 许可证
文档未提及相关许可证信息。
⚠️ 重要提示
- 隐私保护:禁止采集用户个人信息,遵守相关法律法规,避免数据滥用。
- 平台规则:避免频繁操作,防止账号被封禁,使用合理的时间间隔模拟真实用户行为。
- 异常处理:
try:
except Exception as e:
logging.error(f'发生错误:{e}')
page.close()