# -*- coding: utf-8 -*- """ 小红书图文笔记上传示例 展示如何使用XiaoHongShuImageNote类上传图文笔记 """ import sys import os import asyncio from pathlib import Path # 添加项目根目录到系统路径 current_dir = os.path.dirname(os.path.abspath(__file__)) project_root = os.path.dirname(current_dir) sys.path.append(project_root) from conf import BASE_DIR from uploader.xhs_note_uploader.main import ( XiaoHongShuImageNote, xiaohongshu_note_setup ) async def upload_image_note_example(): """图文笔记上传示例""" print("=" * 60) print("小红书图文笔记上传示例") print("=" * 60) # ========== 配置参数 ========== # 标题(最多30字符) title = "今天的下午茶时光☕️" # 正文内容(最多1000字符,支持换行) content = """分享一下今天的下午茶时光~ 这家新开的咖啡馆环境超级好 阳光透过落地窗洒进来 感觉整个人都被治愈了💕 点了他们家的招牌拿铁 还有手工曲奇 味道真的绝了! 推荐给爱喝咖啡的小伙伴们~ #下午茶 #咖啡馆 #生活记录""" # 话题标签(建议最多3个,不需要加#号) tags = ["下午茶", "咖啡馆", "生活记录"] # 图片路径列表(1-9张) # 请将你的图片放在videos目录下,或使用绝对路径 image_paths = [ str(Path(BASE_DIR) / "videos" / "demo.png"), # 示例图片1 # str(Path(BASE_DIR) / "videos" / "photo2.jpg"), # 示例图片2 # str(Path(BASE_DIR) / "videos" / "photo3.jpg"), # 示例图片3 ] # Cookie文件路径 account_file = Path(BASE_DIR) / "cookies" / "xiaohongshu_note" / "account.json" account_file.parent.mkdir(parents=True, exist_ok=True) # 发布时间(0表示立即发布,也可以传入datetime对象) publish_date = 0 # 可选参数 cover_index = 0 # 封面索引(0表示第一张图) filter_name = None # 滤镜名称(如:"清新"、"复古"等) location = "上海市·静安区" # 地点(可选) # 是否使用无头模式(不推荐,建议设为False) headless = False # ========== 检查Cookie ========== print(f"\n[1/3] 检查Cookie...") cookie_valid = await xiaohongshu_note_setup(str(account_file), handle=True) if not cookie_valid: print("❌ Cookie无效,请重新登录") return print("✅ Cookie有效") # ========== 验证图片文件 ========== print(f"\n[2/3] 验证图片文件...") for i, img_path in enumerate(image_paths): if not os.path.exists(img_path): print(f"❌ 图片文件不存在: {img_path}") print(f"提示: 请将图片放在 {Path(BASE_DIR) / 'videos'} 目录下") return print(f"✅ 图片 {i+1}: {os.path.basename(img_path)}") # ========== 创建上传器并执行上传 ========== print(f"\n[3/3] 开始上传图文笔记...") print(f"标题: {title}") print(f"图片数量: {len(image_paths)}") print(f"标签: {tags}") print(f"地点: {location}") print() try: # 创建图文笔记上传器 note = XiaoHongShuImageNote( title=title, content=content, tags=tags, image_paths=image_paths, publish_date=publish_date, account_file=str(account_file), cover_index=cover_index, filter_name=filter_name, location=location, headless=headless ) # 执行上传 await note.main() print("\n" + "=" * 60) print("🎉 图文笔记上传成功!") print("=" * 60) except Exception as e: print("\n" + "=" * 60) print(f"❌ 上传失败: {e}") print("=" * 60) raise async def upload_multiple_notes_example(): """批量上传多个图文笔记示例""" print("=" * 60) print("小红书图文笔记批量上传示例") print("=" * 60) account_file = Path(BASE_DIR) / "cookies" / "xiaohongshu_note" / "account.json" account_file.parent.mkdir(parents=True, exist_ok=True) # 检查Cookie cookie_valid = await xiaohongshu_note_setup(str(account_file), handle=True) if not cookie_valid: print("❌ Cookie无效") return # 笔记列表 notes_config = [ { "title": "早餐分享🍳", "content": "今天的早餐超级丰盛~", "tags": ["早餐", "美食", "健康生活"], "images": [str(Path(BASE_DIR) / "videos" / "demo.png")], }, { "title": "好物推荐💄", "content": "最近入手的宝藏好物分享!", "tags": ["好物推荐", "种草", "护肤"], "images": [str(Path(BASE_DIR) / "videos" / "demo.png")], }, # 可以继续添加更多笔记... ] # 逐个上传 for i, config in enumerate(notes_config): print(f"\n[{i+1}/{len(notes_config)}] 上传笔记: {config['title']}") try: note = XiaoHongShuImageNote( title=config["title"], content=config["content"], tags=config["tags"], image_paths=config["images"], publish_date=0, account_file=str(account_file), headless=False ) await note.main() print(f"✅ 笔记 {i+1} 上传成功") # 上传间隔10-15分钟(避免风控) if i < len(notes_config) - 1: import random wait_time = random.randint(600, 900) # 10-15分钟 print(f"⏰ 等待 {wait_time//60} 分钟后上传下一篇...") await asyncio.sleep(wait_time) except Exception as e: print(f"❌ 笔记 {i+1} 上传失败: {e}") continue print("\n" + "=" * 60) print("🎉 批量上传完成!") print("=" * 60) if __name__ == "__main__": # 运行单个笔记上传示例 asyncio.run(upload_image_note_example()) # 如果需要批量上传,注释掉上面一行,取消下面的注释 # asyncio.run(upload_multiple_notes_example())