6.7 KiB
Raw Permalink Blame History

微信公众号图文上传器使用说明

概述

微信公众号图文上传器支持自动化上传图片和视频到微信公众号,并自动填写标题、内容和标签。

功能特性

  • 自动点击图文创作按钮
  • 支持新页面和当前页面跳转
  • 自动填写文章标题
  • 自动填写内容和标签
  • 支持图片和视频上传
  • 智能识别透明上传按钮
  • 自动发布文章
  • 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,
        headless=False  # 推荐使用有头模式,避免反自动化检测
    )
    
    # 执行上传
    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 封面图片路径(暂未使用)
headless bool 是否使用无头模式,默认 True

4. 无头模式说明

无头模式 (headless=True)

  • 优点:运行时不显示浏览器窗口,适合服务器环境
  • 优点:运行速度更快,占用资源较少
  • 缺点:容易被微信公众号检测和阻止
  • 缺点:页面元素可能无法正确加载
  • ⚠️ 缺点:调试时无法观察浏览器行为

有头模式 (headless=False)

  • 优点:可以观察浏览器操作过程,便于调试
  • 优点:避免反自动化检测,成功率更高
  • 优点:页面元素加载更稳定
  • ⚠️ 缺点:需要图形界面环境
  • ⚠️ 缺点:占用更多系统资源

建议

  • 微信公众号推荐使用 headless=False(避免检测)
  • 开发调试时使用 headless=False
  • 如果必须使用无头模式,请先测试元素是否正常加载

5. 支持的文件格式

图片格式:

  • JPG/JPEG
  • PNG
  • GIF
  • BMP

视频格式:

  • MP4
  • AVI
  • MOV
  • WMV

6. 完整示例

# 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())

注意事项

  1. 首次使用:需要手动登录微信公众号获取 Cookie
  2. 文件路径:确保文件路径正确且文件存在
  3. 网络环境:需要稳定的网络连接
  4. 频率控制避免频繁上传建议间隔30秒以上
  5. Cookie有效期Cookie过期时会自动提示重新登录

故障排除

常见问题

1. Cookie过期

解决方案:重新运行 wechat_setup(account_file, handle=True)

2. 文件上传失败

检查项:
- 文件是否存在
- 文件格式是否支持  
- 文件大小是否符合要求
- 网络连接是否正常

3. 无头模式下找不到页面元素/截图全黑

原因:微信公众号具有反自动化检测机制,在无头模式下可能:
- 页面结构发生变化
- 关键元素被隐藏或动态加载失败
- 触发安全验证机制

解决方案:
- 推荐使用有头模式headless=False
- 程序已添加反检测措施User-Agent、浏览器参数等
- 使用调试方法分析页面状态
- 如果必须使用无头模式,可尝试其他反检测技术

4. 页面元素找不到

原因:微信公众号页面结构变化
解决:等待作者更新选择器或手动调整

5. 浏览器启动失败

# 重新安装 playwright
playwright install chromium

更新日志

  • v1.0.0: 基础功能实现
  • v1.1.0: 支持新页面跳转检测
  • v1.2.0: 优化透明按钮点击逻辑
  • v1.3.0: 改进鼠标悬停机制

技术支持

如遇问题,请检查:

  1. 依赖是否正确安装
  2. 文件路径是否正确
  3. Cookie是否有效
  4. 网络连接是否正常

提示:本工具仅供学习和研究使用,请遵守微信公众号的使用条款。