小红书自动化发布设计

  大童  |   on Saturday, June 21, 2025  |  1660   |  4 minutes

需求 20250621

在发布个人网站(hugo)时,判断markdown文章的layout标签,如果标签为post-xhs,把该篇文章同时发布到小红书中。 因为小红书发布的内容格式等限制,比如必须附加图片/视频,title和content字数限制…..,需考虑:

  1. markdown中的那些内容裁剪到小红书,部分内容放在markdown的tag header上。 文章内容中段前放置:
    <!--xhs-->
    
  2. 内容转化为图片,并套用模版及渲染
  3. 通过selenium来做web操作,完成自动化发布。

整个过程串流到个人网站的自动化发布过程中。一条指令完成网站发布和小红书发布。 如有必要,使用QT6作python前端

另,需考虑:

  • 小红书登录需要能够记录cookie,简化自动化登录过程。
  • 自动化发布过程,应模拟用户参数(用户环境指纹等),规避平台拦截。
  • 所有文章在post目录下,哪些需要发布到小红书?定一个原则:本次git push前新增的文件。 本次git push修改的文件,如重新发布会造成文章重复发布。能否判断以前已发布的文章,对修改的内容在小红书进行修改。 ( listgitfile.py )

todolist:

  • 调用大模型进行内容、图片、视频创作。 AI大模型
  • 字体和模版随机选择,引入随机性
  • 内容框架 & 内容分页展示,文字 + 装饰图,装饰图大模型自动生成。
  • markdown header 中定义模版

设计

  1. 发布markdown时,解析layout为:post-xhs,增加小红书发布流程。
  2. 解析markdown内容,获取小红书所需字段放在特定数据结构json/dict中,数据结构定义
  3. 数据结构中的内容渲染。
  4. 三类内容发布:
  • 图文内容在小红书WEB版本上入口相同“上传图文”。 图文内容:解析markdown中正文中的图片上传。
  • 视频内容使用“上传视频”入口。视频从markdown正文中获取上传。
  • 文字内容在小红书WEB版本上入口相同“上传图文”。
    • 使用markdown header中的image tag定义的图片。
    • 文字内容转化为图片(套用模版,渲染等)。
  1. 使用selenium API操作上传发布。 小红书操作的CSS选择器

模块

  • push时检查post下新增的markdown文件。 – listgitfile.py ✅
  • 文字内容转化为渲染后的图片。 – texttoimage.py ✅
  • 小红书操作

小红书CSS选择器

STEP 1

xhspublisher.png

CSS选择器 ① div.publish-video .btn ② div.group-list .publish-card:nth-child(1) .image ③ div.group-list .publish-card:nth-child(2) .image

STEP 21 发布笔记

xhspublisher-2.png

CSS选择器 ① div.publish-video .btn ② div.outarea.upload-c .creator-tab:nth-child(1) ③ div.outarea.upload-c .creator-tab:nth-child(3) ④ div.outarea.upload-c .upload-content button

STEP 22 上传视频

点击上传视频后( ② div.outarea.upload-c .creator-tab:nth-child(1) ) xhspublisher-3.png xhspublisher-6.png

CSS选择器 ① .cover-container .stage div:first-child 判断出现文字“上传成功”,设计为异步等待?不阻塞标题及内容输入等其他操作。 ② .titleInput .d-text ③ #quillEditor.ql-editor ④ #topicBtn ⑤ .media-extension .plugin:nth-child(2) .d-select-placeholder ⑥ .media-settings>div>div:nth-child(2) .d-select-description ⑦ .el-radio-group label:nth-child(1) input - 立即发布 .el-radio-group label:nth-child(2) input - 定时发布 .el-radio-group .date-picker input - 时间2025-06-21 15:14 ⑧ .publishBtn

STEP 31 上传图片

点击上传图片后(③ div.outarea.upload-c .creator-tab:nth-child(3) ) xhspublisher-7.png

CSS选择器 ① .publish-c .media-area-new .img-upload-area .entry ② .titleInput .d-text ③ #quillEditor .ql-editor ④ #topicBtn ⑤ .media-extension .plugin:nth-child(2) .d-select-placeholder ⑥ .media-settings>div>div:nth-child(2) .d-select-description ⑦ .el-radio-group label:nth-child(1) input - 立即发布 .el-radio-group label:nth-child(2) input - 定时发布 .el-radio-group .date-picker input - 时间2025-06-21 15:14 ⑧ .publishBtn

Markdown解析

数据结构

数据来源

  • markdown header 解析markdown笔记的header部分: image : 封面图片,文字内容的封面图片 xhstitle : 新增,小红书标题 xhsdate : 定时发布时间,不存在或留空表示立即发布 xhstags : 新增,作为小红书的#tags,并加入原header中的tags内容。 xhswhere : 小红书中你在哪里/地点。 xhsopen : yes-公开可见,no-仅自己可见

  • markdown content 解析markdown内容,并获取[xhs内容/]

  • 数据结构 xhsdata = { “filename”: { “title”: “Labubu爆火现象”, “date”: “2025-06-19 11:00”, “tags”: [“潮玩”,“labubu”……], “where”: “杭州市西湖风景名胜区”, “open”: “yes”, “content”: [“line1”,“line2”,“line3”……] } }

小红书发布流程

  • 利用selenium登录,首次输入phone number,并记录cookie。以后尝试读取cookies自动登录,无法登陆则重新输入phone number。
  • 发布文章

AI大模型

使用豆包火山引擎

CONFIG

解析markdown内容时,每页内容的行数和总字数限制 page-line-number-limit page-line-word-count-limit page-word-count-limit

链接&参考