🚀 小紅書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
📚 詳細文檔
代碼結構
- 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['title'], note_info['author'],
note_info['content'], note_info['likes']))
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()
總結
通過Playwright框架實現的小紅書MCP工具,能夠有效模擬用戶行為,幫助完成內容發佈、數據採集和互動操作。合理使用該工具,可以顯著提升運營效率,同時需注意遵守平臺規則和法律法規。