当前位置:网站首页 >  攻略

用Python搭建小红书私信关键词监控自动话术引流实操技术指南

时间:2026年06月07日 01:40:26 来源:易频IT社区

1. 前置准备:准备好所有需要的账号与工具

1.1 必备账号

  • 1个实名认证的小红书普通/企业号(企业号私信更稳定,但普通号也能用)
  • 1个阿里云/腾讯云的短信验证过的阿里云盘/TempMail临时邮箱号(用于注册小红书自动化工具Cookie提取插件)

1.2 工具安装(按顺序操作)

第一步:安装Python3.10.x(必须是这个版本,高版本低版本兼容性差)

直接访问官网下载Windows/Mac/Linux对应3.10.12的安装包:https://www.python.org/downloads/release/python-31012/

Windows/Mac安装时,务必勾选「Add Python 3.10 to PATH」,安装完成后打开终端/命令提示符输入以下命令验证:

``` python -V pip -V ```

如果显示版本号为3.10.x,说明安装成功。

第二步:安装核心自动化库

终端/命令提示符依次输入以下3条命令,全部复制粘贴即可:

``` pip install selenium==4.10.0 -i https://pypi.tuna.tsinghua.edu.cn/simple pip install undetected-chromedriver==3.5.3 -i https://pypi.tuna.tsinghua.edu.cn/simple pip install webdriver-manager==4.0.0 -i https://pypi.tuna.tsinghua.edu.cn/simple ```

第三步:安装Chrome浏览器与Cookie提取插件

用Python搭建小红书私信关键词监控自动话术引流实操技术指南

Chrome浏览器下载:https://www.google.cn/chrome/

安装完成后,访问Chrome网上应用店下载「EditThisCookie」插件并安装:https://chromewebstore.google.com/detail/editthiscookie/fngmhnnpilhplaeedifhccceomclgfbg

2. 核心配置:获取Cookie并编写目标话术库

2.1 获取小红书有效Cookie

  • 打开Chrome浏览器,无痕模式访问https://www.xiaohongshu.com/explore,登录你的小红书账号
  • 登录成功后,点击浏览器右上角的EditThisCookie插件图标,选择「Export」→「JSON」,复制弹出的所有内容
  • 在桌面新建一个名为「xiaohongshu_data」的文件夹,里面新建一个cookies.json的文件,粘贴刚才复制的JSON内容,保存关闭

2.2 编写可直接复制的目标话术库

在xiaohongshu_data文件夹里新建scripts.txt文件,内容格式要求:每条话术单独一行,用{{nickname}}代表对方昵称的占位符,示例:

``` 嗨{{nickname}},我看你在笔记里问的XX问题,我之前整理了一份免费的XX教程,要不要发给你呀? {{nickname}}下午好~XX这个坑我踩过很多次,最后总结了3个避坑技巧,需要的话戳我回复“避坑”就可以啦! ```

注意:每条话术字数控制在20-50字,不要带明显的二维码、外链。

2.3 配置监控关键词与私信间隔

在xiaohongshu_data文件夹里新建config.py文件,完整可直接复制的内容如下:

``` 监控的笔记关键词(可以设置多个,用英文逗号分隔) KEYWORDS = "Python入门,Python副业,Python兼职" 每次监控的笔记数量(建议10-20,太多容易被封) MONITOR_COUNT = 15 每条私信的间隔时间(单位:秒,建议60-120,模拟真人) MESSAGE_INTERVAL = 90 桌面xiaohongshu_data文件夹的绝对路径(Windows/Mac自行替换) Windows绝对路径示例:C:\\Users\\你的用户名\\Desktop\\xiaohongshu_data Mac绝对路径示例:/Users/你的用户名/Desktop/xiaohongshu_data DATA_PATH = "" ```

注意:绝对路径必须正确,Windows路径要把单斜杠改成双斜杠。

3. 编写完整可执行的Python代码

在桌面新建一个auto_xiaohongshu.py文件,完整可直接复制的内容如下:

``` import time import random import json import os from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from webdriver_manager.chrome import ChromeDriverManager import undetected_chromedriver as uc from config import KEYWORDS, MONITOR_COUNT, MESSAGE_INTERVAL, DATA_PATH def load_cookies(driver): cookies_file = os.path.join(DATA_PATH, "cookies.json") with open(cookies_file, "r", encoding="utf-8") as f: cookies = json.load(f) for cookie in cookies: if "expiry" in cookie: del cookie["expiry"] driver.add_cookie(cookie) driver.refresh() def load_scripts(): scripts_file = os.path.join(DATA_PATH, "scripts.txt") with open(scripts_file, "r", encoding="utf-8") as f: scripts = [line.strip() for line in f if line.strip()] return scripts def main(): 初始化浏览器 options = uc.ChromeOptions() options.add_argument("--disable-blink-features=AutomationControlled") options.add_argument("--start-maximized") driver = uc.Chrome(service=Service(ChromeDriverManager(version="114.0.5735.90").install()), options=options) driver.implicitly_wait(10) wait = WebDriverWait(driver, 20) try: 访问小红书并加载Cookie driver.get("https://www.xiaohongshu.com/explore") load_cookies(driver) time.sleep(3) 加载话术库 scripts = load_scripts() if not scripts: print("❌ scripts.txt为空,请先添加话术") return 遍历监控关键词 keywords = KEYWORDS.split(",") for keyword in keywords: keyword = keyword.strip() print(f"\n🔍 开始监控关键词:{keyword}") 搜索关键词 search_box = wait.until(EC.presence_of_element_located((By.XPATH, '//input[@placeholder="搜索小红书"]'))) search_box.clear() search_box.send_keys(keyword) time.sleep(1) search_box.send_keys("\n") time.sleep(5) 获取笔记列表 note_items = wait.until(EC.presence_of_all_elements_located((By.XPATH, '//div[@class="feeds-page yipinkpckgj-nqws-tgg8"]//div[contains(@class, "note-item")]')))[:MONITOR_COUNT] print(f"📚 找到{len(note_items)}条相关笔记") 遍历每条笔记 for i, note_item in enumerate(note_items): try: 打开笔记新标签页 original_window = driver.current_window_handle note_link = note_item.find_element(By.XPATH, './/a[@class="title yipinkp0tnn-fsry-vwew"]') driver.execute_script("window.open(arguments[0].href);", note_link) 切换到新标签页 driver.switch_to.window([window for window in driver.window_handles if window != original_window][0]) time.sleep(random.uniform(2, 4)) 获取评论区用户(优先找带提问关键词的评论者) try: 加载评论 driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") time.sleep(random.uniform(2, 3)) comment_users = driver.find_elements(By.XPATH, '//div[contains(@class, "comment-item")]//a[@class="user-name yipinkpx535-jqtn-x4gp"]')[:3] if not comment_users: 没有评论就找作者 comment_users = [driver.find_element(By.XPATH, '//div[contains(@class, "author-wrapper")]//a[@class="user-name yipinkpx535-jqtn-x4gp"]')] except: 没有评论/作者就找点赞的人(简化版) comment_users = [driver.find_element(By.XPATH, '//div[contains(@class, "author-wrapper")]//a[@class="user-name yipinkpx535-jqtn-x4gp"]')] 给用户发私信 for user in comment_users[:1]: 每条笔记只给1个用户发,降低风险 try: 获取用户昵称 nickname = user.text 点击用户头像/昵称进入主页 user.click() time.sleep(random.uniform(3, 5)) 点击私信按钮 message_btn = wait.until(EC.presence_of_element_located((By.XPATH, '//button[contains(text(), "私信") or contains(@class, "message-btn")]'))) message_btn.click() time.sleep(random.uniform(2, 3)) 输入话术 message_box = wait.until(EC.presence_of_element_located((By.XPATH, '//textarea[contains(@placeholder, "发送消息")]'))) selected_script = random.choice(scripts).replace("{{nickname}}", nickname) message_box.send_keys(selected_script) time.sleep(random.uniform(1, 2)) 发送消息(可以手动取消注释最后一行测试,不测试先注释) send_btn = wait.until(EC.element_to_be_clickable((By.XPATH, '//button[contains(text(), "发送")]'))) send_btn.click() print(f"✅ 已准备向【{nickname}】发送:{selected_script}") time.sleep(random.uniform(MESSAGE_INTERVAL-10, MESSAGE_INTERVAL+10)) except Exception as e: print(f"⚠️ 向用户发私信失败:{str(e)}") finally: 返回笔记列表标签页 driver.close() driver.switch_to.window(original_window) time.sleep(random.uniform(1, 2)) except Exception as e: print(f"⚠️ 处理第{i+1}条笔记失败:{str(e)}") continue except Exception as e: print(f"❌ 主程序出错:{str(e)}") finally: 关闭浏览器 time.sleep(3) driver.quit() print("\n👋 程序运行结束") if __name__ == "__main__": if not DATA_PATH: print("❌ 请先在config.py中配置DATA_PATH") else: main() ```

4. 零风险测试与正式运行

4.1 零风险测试

  • 打开auto_xiaohongshu.py文件,找到最后有 发送消息注释的那两行,确保它们是被注释的状态(前面加了)
  • 打开终端/命令提示符,进入桌面目录:cd Desktop
  • 运行程序:python auto_xiaohongshu.py
  • 观察浏览器的操作是否正常,终端是否输出准备发送的话术

4.2 正式运行

  • 测试无误后,取消注释auto_xiaohongshu.py中 发送消息下面的两行
  • 用自己的小号做1-2次测试,确保小号能收到消息
  • 测试通过后,每天运行1-2次,每次不要超过1小时,间隔时间不要低于config.py里设置的MESSAGE_INTERVAL

相关推荐

最新

热门

推荐

精选

标签

易频IT社区是综合性互联网IT技术门户网站,专注分享网络技术、服务器运维、网络安全、编程开发、系统架构、云计算、大数据等行业干货,实时更新IT行业资讯、零基础教程、实战案例,为IT从业者、技术爱好者提供专业的学习交流平台。

Copyright © 2021-2026 易频IT社区. All Rights Reserved. 备案号:闽ICP备2023013482号 网站地图