小红书图文发布初版(已优化正文和标签)

This commit is contained in:
Shuang_Dong 2025-10-14 17:56:43 +08:00
parent de8fba8a77
commit 377e34c651
6 changed files with 1494 additions and 3 deletions

View File

@ -0,0 +1,263 @@
# -*- coding: utf-8 -*-
# 小红书图文上传脚本 - 智能适配单图和多图
import sys
import os
from pathlib import Path
# 获取当前脚本所在目录
current_dir = os.path.dirname(os.path.abspath(__file__))
# 项目根目录是当前目录的上一级因为examples目录和conf.py同级
project_root = os.path.dirname(current_dir)
# 将项目根目录添加到系统路径
sys.path.append(project_root)
import asyncio
import re
from datetime import datetime, timedelta
from conf import BASE_DIR
from uploader.xiaohongshu_uploader.main import XiaoHongShuImage, xiaohongshu_setup
from utils.files_times import generate_schedule_time_next_day
def get_image_groups_from_folder(images_folder):
"""
从文件夹中智能获取图片组
支持两种方式
1. 单独的图片文件每个图片一个图文
2. 以数字结尾的图片组旅行1.jpg, 旅行2.jpg, 旅行3.jpg -> 一个图文包含3张图
"""
images_folder = Path(images_folder)
if not images_folder.exists():
print(f"图片文件夹不存在: {images_folder}")
return []
# 获取所有图片文件
image_extensions = ['.jpg', '.jpeg', '.png', '.webp', '.bmp']
all_images_set = set() # 使用集合去重
for ext in image_extensions:
# 搜索小写扩展名
for img in images_folder.glob(f"*{ext}"):
all_images_set.add(img.resolve()) # 使用绝对路径去重
# 搜索大写扩展名
for img in images_folder.glob(f"*{ext.upper()}"):
all_images_set.add(img.resolve()) # 使用绝对路径去重
all_images = list(all_images_set) # 转换回列表
if not all_images:
print(f"{images_folder} 中未找到图片文件")
return []
# 按文件名分组
image_groups = {}
for image_path in all_images:
filename = image_path.stem # 不包含扩展名的文件名
# 检查文件名是否以数字结尾旅行1, 美食2
match = re.match(r'^(.+?)(\d+)$', filename)
if match:
# 有数字后缀,按基础名称分组
base_name = match.group(1)
number = int(match.group(2))
if base_name not in image_groups:
image_groups[base_name] = []
image_groups[base_name].append((number, image_path))
else:
# 没有数字后缀,单独成组
if filename not in image_groups:
image_groups[filename] = []
image_groups[filename].append((1, image_path))
# 整理分组结果
result_groups = []
for base_name, images in image_groups.items():
# 按数字排序
images.sort(key=lambda x: x[0])
image_paths = [img[1] for img in images]
# 判断是单图还是多图
if len(image_paths) == 1:
print(f"发现单图: {base_name} - {image_paths[0].name}")
else:
print(f"发现多图组: {base_name} - {len(image_paths)} 张图片")
for i, path in enumerate(image_paths, 1):
print(f" {i}. {path.name}")
result_groups.append({
'base_name': base_name,
'image_paths': image_paths,
'count': len(image_paths),
'type': 'multi' if len(image_paths) > 1 else 'single'
})
return result_groups
def get_image_metadata(base_name, images_folder):
"""
根据基础名称获取图文元数据
查找对应的txt文件旅行.txt 对应 旅行1.jpg, 旅行2.jpg 或单独的 旅行.jpg
"""
txt_file = Path(images_folder) / f"{base_name}.txt"
if txt_file.exists():
with open(txt_file, 'r', encoding='utf-8') as f:
lines = f.readlines()
# 第一行:标题
title = lines[0].strip() if len(lines) >= 1 else base_name
# 第二行:标签
if len(lines) >= 2:
tags_line = lines[1].strip()
# 智能识别标签格式
if tags_line.startswith('#'):
# 空格分隔格式:#美食 #甜品 #生活
tags = []
for tag in tags_line.split():
tag = tag.strip()
if tag and tag.startswith('#'):
tag_content = tag[1:].strip() # 移除#号
if tag_content:
tags.append(tag_content)
else:
# 逗号分隔格式:美食,甜品,生活 或 美食,甜品,生活
tags = [tag.strip() for tag in tags_line.replace('', ',').split(',') if tag.strip()]
else:
tags = ['生活', '分享']
# 第三行:地点(可选)
location = None
if len(lines) >= 3:
location_line = lines[2].strip()
if location_line: # 只有非空才设置地点
location = location_line
# 第四行及以后:正文内容(可选)
content = None
if len(lines) >= 4:
# 从第四行开始的所有内容作为正文
content_lines = [line.rstrip() for line in lines[3:]]
# 移除开头和结尾的空行
while content_lines and not content_lines[0]:
content_lines.pop(0)
while content_lines and not content_lines[-1]:
content_lines.pop()
if content_lines:
content = '\n'.join(content_lines)
else:
# 没有对应的txt文件使用默认值
title = base_name
tags = ['生活', '分享']
location = None
content = None
return title, tags, location, content
if __name__ == '__main__':
print("🎯 小红书图文上传工具 - 智能适配单图/多图")
print("=" * 50)
# 配置
images_folder = Path(BASE_DIR) / "images"
account_file = Path(BASE_DIR / "cookies" / "xiaohongshu_uploader" / "account.json")
# 检查文件夹和账号文件
if not images_folder.exists():
print(f"❌ 图片文件夹不存在: {images_folder}")
print("请创建images文件夹并放入要上传的图片文件")
exit(1)
if not account_file.exists():
print("❌ 账号文件不存在,请先运行 get_xiaohongshu_cookie.py 获取登录凭证")
exit(1)
# 智能获取图片分组
print("🔍 正在扫描图片文件...")
image_groups = get_image_groups_from_folder(images_folder)
if not image_groups:
print("❌ 未找到任何图片文件")
print("支持的格式: jpg, jpeg, png, webp, bmp")
exit(1)
# 统计信息
total_groups = len(image_groups)
single_count = sum(1 for group in image_groups if group['type'] == 'single')
multi_count = sum(1 for group in image_groups if group['type'] == 'multi')
total_images = sum(group['count'] for group in image_groups)
print(f"\n📊 扫描结果:")
print(f" • 总图文数: {total_groups}")
print(f" • 单图图文: {single_count}")
print(f" • 多图图文: {multi_count}")
print(f" • 总图片数: {total_images}")
# 生成定时发布时间每天下午4点发布1个图文
print(f"\n⏰ 生成发布时间表...")
publish_datetimes = generate_schedule_time_next_day(total_groups, 1, daily_times=[16])
# 检查cookie
print("🔐 验证登录状态...")
cookie_setup = asyncio.run(xiaohongshu_setup(account_file, handle=False))
if not cookie_setup:
print("❌ Cookie验证失败请先运行 get_xiaohongshu_cookie.py 获取登录凭证")
exit(1)
print("✅ 登录状态验证成功")
# 逐个上传图文组
print(f"\n🚀 开始上传图文...")
print("=" * 50)
for index, group in enumerate(image_groups):
try:
base_name = group['base_name']
image_paths = group['image_paths']
image_count = group['count']
group_type = group['type']
# 获取图文信息
title, tags, location, content = get_image_metadata(base_name, images_folder)
print(f"\n📝 第 {index + 1}/{total_groups} 个图文")
print(f" 类型: {'🖼️ 单图' if group_type == 'single' else '🖼️ ×' + str(image_count) + ' 多图'}")
print(f" 名称: {base_name}")
print(f" 标题: {title}")
print(f" 标签: {', '.join(tags)}")
print(f" 地点: {location if location else '未设置'}")
print(f" 正文: {len(content) if content else 0} 字符")
print(f" 发布: {publish_datetimes[index]}")
# 创建图文上传实例(自动适配单图/多图)
app = XiaoHongShuImage(
title=title,
image_paths=[str(path) for path in image_paths], # 自动适配单张或多张图片
tags=tags,
publish_date=publish_datetimes[index],
account_file=account_file,
location=location,
content=content,
headless=False
)
# 执行上传
print(f" 🔄 正在上传...")
asyncio.run(app.main(), debug=False)
type_desc = f"单图" if group_type == 'single' else f"{image_count}张图"
print(f" ✅ 图文《{title}》({type_desc}) 上传完成")
except Exception as e:
print(f" ❌ 上传图文组 {base_name} 时出错: {e}")
continue
print(f"\n🎉 所有图文上传完成!")
print(f"📊 处理结果: {total_groups} 个图文组,{total_images} 张图片")
print("=" * 50)

View File

@ -38,5 +38,5 @@ if __name__ == '__main__':
# if thumbnail_path.exists():
# app = XiaoHongShuVideo(title, file, tags, publish_datetimes[index], account_file, thumbnail_path=thumbnail_path)
# else:
app = XiaoHongShuVideo(title, file, tags, publish_datetimes[index], account_file, headless=True) # 推荐使用有头模式
app = XiaoHongShuVideo(title, file, tags, publish_datetimes[index], account_file, headless=False) # 推荐使用有头模式
asyncio.run(app.main(), debug=False)

231
images/README.md Normal file
View File

@ -0,0 +1,231 @@
# 小红书图文上传 - 图片文件夹
这个文件夹用于存放要上传到小红书的图片文件。
## 🎯 **支持的上传方式**
- **单图上传**:每张图片单独发布一个图文
- **多图上传**多张图片组合成一个图文最多9张
## 📁 文件结构
```
images/
├── README.md # 说明文件
├── 图片1.jpg # 图片文件
├── 图片1.txt # 对应的标题和标签文件(可选)
├── 图片2.png # 另一张图片
├── 图片2.txt # 对应的标题和标签文件
└── ...
```
## 🖼️ 支持的图片格式
- **JPG/JPEG** - 推荐格式
- **PNG** - 支持透明背景
- **WEBP** - 现代格式,文件更小
## 📝 标题和标签配置
为每张图片创建同名的 `.txt` 文件来配置标题和标签:
### 文件格式
```
第一行:图文标题
第二行:标签(支持两种格式)
第三行:地点信息(可选,留空则不设置地理位置)
第四行及以后:正文内容(可选,支持多行长文本)
```
### 📋 **标签格式支持**
#### **格式1逗号分隔**
```
美食,甜品,蛋糕,下午茶,生活
```
#### **格式2空格分隔#号**
```
#美食 #甜品 #蛋糕 #下午茶 #生活
```
**注意**:系统会自动识别格式并处理,两种格式效果相同。
### 🎯 **智能标签建议选择**
系统支持智能标签建议选择功能:
#### **选择策略**
1. **精确匹配优先**:如果找到与输入标签完全一致的建议,优先选择
2. **包含匹配备选**:如果没有精确匹配,选择包含关键词的相关建议
3. **自动生成新标签**:如果没有任何匹配的建议,系统会自动生成新标签
#### **处理流程**
```
输入标签 → 等待建议加载 → 查找最佳匹配 → 选择建议或生成新标签
```
#### **示例**
- 输入 `#广州旅游` → 找到 `#广州旅游 16.8亿人浏览` → 自动选择
- 输入 `#美食分享` → 找到 `#美食分享日常` → 选择相关建议
- 输入 `#我的原创标签` → 无匹配建议 → 生成新标签
### 示例文件:`美食分享.txt`
```
今日美食推荐 - 超好吃的蛋糕
美食,甜品,蛋糕,下午茶,生活
北京市
今天发现了一家超棒的蛋糕店!🍰
这家店的招牌是巧克力慕斯蛋糕,
口感丰富,甜而不腻,
搭配他们家的手冲咖啡简直完美!
店里的装修也很有格调,
很适合和朋友一起来聊天放松。
下次还想再来尝试其他口味。
推荐给喜欢甜品的朋友们!
你们有什么好吃的蛋糕店推荐吗?
```
### 不设置地理位置的示例:`生活分享.txt`
```
今天的心情特别好
生活,分享,心情
今天阳光明媚,心情特别好!
和朋友一起度过了愉快的一天。
```
**注意**:第三行留空,系统会自动跳过地理位置设置。
## 📸 **多图上传功能**
### 文件命名规则
对于多图上传,使用以下命名规则:
```
旅行1.jpg # 第1张图
旅行2.jpg # 第2张图
旅行3.jpg # 第3张图
旅行.txt # 对应的文本文件
```
### 多图示例:`旅行.txt`
```
三亚海边度假之旅
旅行,度假,海边,三亚,美景
三亚市
这次三亚之旅真的太棒了!🏖️
第一天:抵达三亚,入住海景酒店
第二天:天涯海角,椰梦长廊漫步
第三天:亚龙湾海滩,享受阳光沙滩
每一刻都是美好的回忆!
```
### 使用脚本
使用 `upload_images_to_xiaohongshu.py` - **智能适配单图和多图**
```bash
python examples/upload_images_to_xiaohongshu.py
```
### 🤖 **智能适配规则**
脚本会自动识别文件命名规则:
#### **单图模式**
```
美食.jpg ← 单独发布一个图文
美食.txt ← 对应的文本文件
```
#### **多图模式**
```
旅行1.jpg ┐
旅行2.jpg ├─ 自动组合成一个图文
旅行3.jpg ┘
旅行.txt ← 对应的文本文件
```
#### **混合模式**
```
美食.jpg ← 单图图文
旅行1.jpg ┐
旅行2.jpg ├─ 多图图文
旅行3.jpg ┘
生活.jpg ← 单图图文
```
**所有图片会被智能分组并按计划发布**
### 配置说明
#### 📍 地点信息(第三行)
- **格式要求**:城市名(如"北京市"、"上海市"、"广州市"
- **可选设置**:可以留空或不写
- **自动处理**:如果不设置地点,系统将跳过位置设置
#### 📝 正文内容(第四行及以后)
- **支持长文本**:可以写多行内容,支持换行
- **内容丰富**:可以包含表情符号、问句、描述等
- **自动处理**:如果不写正文,系统将使用标题作为默认内容
- **格式保持**:会保持原有的换行和段落格式
- **标签位置**:标签会自动添加在正文内容的后面,用空格分隔
## 🚀 使用方法
1. **准备图片**:将要上传的图片放入此文件夹
2. **配置信息**:为每张图片创建对应的 `.txt` 文件(可选)
3. **运行脚本**:执行 `python examples/upload_image_to_xiaohongshu.py`
### ⏰ 定时发布说明
- **默认设置**每天下午4点发布1个图文
- **自动排期**:多个图文会按天数顺序排期
- **发布逻辑**:与视频发布保持一致的定时机制
## 📋 注意事项
- 如果没有 `.txt` 文件,将使用图片文件名作为标题
- 标签可以用逗号或中文逗号分隔
- 建议图片尺寸为正方形或竖屏比例
- 单个图文最多支持9张图片
## 🔧 高级配置
### 单张图片上传
```python
from uploader.xiaohongshu_uploader.main import XiaoHongShuImage
app = XiaoHongShuImage(
title="图文标题",
image_paths=["path/to/image.jpg"],
tags=["标签1", "标签2"],
publish_date=0, # 0表示立即发布
account_file="cookies/xiaohongshu_uploader/account.json",
location="北京市" # 地点信息(可选)
)
```
### 多张图片上传
```python
app = XiaoHongShuImage(
title="多图合集",
image_paths=[
"path/to/image1.jpg",
"path/to/image2.jpg",
"path/to/image3.jpg"
],
tags=["合集", "分享"],
publish_date=0,
account_file="cookies/xiaohongshu_uploader/account.json",
location="上海市" # 地点信息(可选)
)
```
### 地点信息配置
- **参数名称**: `location`
- **数据类型**: 字符串或None
- **示例值**: "北京市"、"上海市"、"广州市"、"深圳市"
- **默认值**: None不设置地点
- **注意事项**: 地点名称需要是小红书支持的有效地点

View File

@ -20,7 +20,7 @@ def sign_local(uri, data=None, a1="", web_session=""):
chromium = playwright.chromium
# 如果一直失败可尝试设置成 False 让其打开浏览器,适当添加 sleep 可查看浏览器状态
browser = chromium.launch(headless=True)
browser = chromium.launch(headless=False)
browser_context = browser.new_context()
browser_context.add_init_script(path=stealth_js_path)

View File

@ -1,5 +1,8 @@
from pathlib import Path
from conf import BASE_DIR
from .main import XiaoHongShuVideo, XiaoHongShuImage, xiaohongshu_setup
Path(BASE_DIR / "cookies" / "xiaohongshu_uploader").mkdir(exist_ok=True)
Path(BASE_DIR / "cookies" / "xiaohongshu_uploader").mkdir(exist_ok=True)
__all__ = ['XiaoHongShuVideo', 'XiaoHongShuImage', 'xiaohongshu_setup']

File diff suppressed because it is too large Load Diff