微信公众号图文上传器使用说明
概述
微信公众号图文上传器支持自动化上传图片和视频到微信公众号,并自动填写标题、内容和标签。
功能特性
- ✅ 自动点击图文创作按钮
- ✅ 支持新页面和当前页面跳转
- ✅ 自动填写文章标题
- ✅ 自动填写内容和标签
- ✅ 支持图片和视频上传
- ✅ 智能识别透明上传按钮
- ✅ 自动发布文章
- ✅ Cookie自动管理
安装依赖
pip install playwright
pip install asyncio
playwright install chromium
使用方法
1. 获取微信公众号 Cookies
第一次使用时,程序会自动打开浏览器让您登录:
from uploader.wechat_public_uploader.main import wechat_setup
import asyncio
async def get_cookies():
account_file = "cookies/wechat_uploader/account.json"
await wechat_setup(account_file, handle=True)
asyncio.run(get_cookies())
2. 基本使用示例
import asyncio
from pathlib import Path
from datetime import datetime
from playwright.async_api import async_playwright
from uploader.wechat_public_uploader.main import WechatVideo, wechat_setup
async def upload_to_wechat():
# Cookie 文件路径
account_file = "cookies/wechat_uploader/account.json"
# 确保 Cookie 有效
if not await wechat_setup(account_file, handle=True):
print("Cookie 设置失败")
return
# 创建上传对象
wechat_video = WechatVideo(
title="我的文章标题",
file_path="videos/demo.png", # 图片或视频路径
tags=["标签1", "标签2", "标签3"],
publish_date=datetime.now(),
account_file=account_file
)
# 执行上传
async with async_playwright() as playwright:
await wechat_video.upload(playwright)
# 运行
asyncio.run(upload_to_wechat())
3. 参数说明
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| title | str | 是 | 文章标题 |
| file_path | str | 是 | 图片/视频文件路径 |
| tags | list | 否 | 标签列表 |
| publish_date | datetime | 否 | 发布时间 |
| account_file | str | 是 | Cookie文件路径 |
| thumbnail_path | str | 否 | 缩略图路径(暂未使用) |
4. 支持的文件格式
图片格式:
- JPG/JPEG
- PNG
- GIF
- BMP
视频格式:
- MP4
- AVI
- MOV
- WMV
5. 完整示例
# examples/upload_video_to_wechat.py
import asyncio
from pathlib import Path
from datetime import datetime, timedelta
from playwright.async_api import async_playwright
from uploader.wechat_public_uploader.main import wechat_setup, WechatVideo
async def batch_upload():
"""批量上传示例"""
account_file = "cookies/wechat_uploader/account.json"
# 确保登录
if not await wechat_setup(account_file, handle=True):
return
# 定义上传列表
upload_list = [
{
"title": "技术分享:如何使用Python自动化",
"file_path": "videos/python_tutorial.mp4",
"tags": ["Python", "自动化", "编程"],
},
{
"title": "AI最新进展分享",
"file_path": "videos/ai_news.png",
"tags": ["AI", "人工智能", "科技"],
}
]
async with async_playwright() as playwright:
for item in upload_list:
wechat_video = WechatVideo(
title=item["title"],
file_path=item["file_path"],
tags=item["tags"],
publish_date=datetime.now(),
account_file=account_file
)
try:
await wechat_video.upload(playwright)
print(f"✅ 成功上传: {item['title']}")
# 等待一段时间避免频繁操作
await asyncio.sleep(30)
except Exception as e:
print(f"❌ 上传失败: {item['title']}, 错误: {e}")
if __name__ == '__main__':
asyncio.run(batch_upload())
注意事项
- 首次使用:需要手动登录微信公众号获取 Cookie
- 文件路径:确保文件路径正确且文件存在
- 网络环境:需要稳定的网络连接
- 频率控制:避免频繁上传,建议间隔30秒以上
- Cookie有效期:Cookie过期时会自动提示重新登录
故障排除
常见问题
1. Cookie过期
解决方案:重新运行 wechat_setup(account_file, handle=True)
2. 文件上传失败
检查项:
- 文件是否存在
- 文件格式是否支持
- 文件大小是否符合要求
- 网络连接是否正常
3. 页面元素找不到
原因:微信公众号页面结构变化
解决:等待作者更新选择器或手动调整
4. 浏览器启动失败
# 重新安装 playwright
playwright install chromium
更新日志
- v1.0.0: 基础功能实现
- v1.1.0: 支持新页面跳转检测
- v1.2.0: 优化透明按钮点击逻辑
- v1.3.0: 改进鼠标悬停机制
技术支持
如遇问题,请检查:
- 依赖是否正确安装
- 文件路径是否正确
- Cookie是否有效
- 网络连接是否正常
提示:本工具仅供学习和研究使用,请遵守微信公众号的使用条款。