# 自动化视频上传脚本模板 ```javascript // upload.js - 通用的视频上传脚本 const { chromium } = require('playwright'); const fs = require('fs'); class VideoUploader { constructor(platform, config) { this.platform = platform; this.config = config; this.browser = null; this.context = null; this.page = null; } async init() { this.browser = await chromium.launch({ headless: false }); this.context = await this.browser.newContext({ userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X)', viewport: { width: 390, height: 844 }, }); this.page = await this.context.newPage(); } async login(username, password) { await this.page.goto(this.config.entry); await this.page.fill('input[type="text"]', username); await this.page.fill('input[type="password"]', password); await this.page.click('button[type="submit"]'); // 等待验证码输入(需手动) await this.page.waitForTimeout(10000); } async upload(videoPath, { title, description, cover }) { // 1. 选择文件 await this.page.setInputFiles(this.config.fileInput, videoPath); // 2. 等待上传 await this.page.waitForTimeout(this.config.waitForUpload); // 3. 填写标题 await this.page.fill(this.config.titleInput, title); // 4. 填写描述 if (this.config.descInput) { await this.page.fill(this.config.descInput, description); } // 5. 选择封面(如果有) if (cover && this.config.coverBtn) { await this.page.click(this.config.coverBtn); await this.page.setInputFiles('input[type="file"]', cover); } // 6. 发布 await this.page.click(this.config.publishBtn); // 7. 等待发布成功 await this.page.waitForTimeout(5000); return { success: true, platform: this.platform }; } async close() { await this.browser.close(); } // 保存登录态 async saveState(path) { await this.context.storageState({ path }); } // 加载登录态 async loadState(path) { if (fs.existsSync(path)) { this.context = await this.browser.newContext({ storageState: path, userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X)', viewport: { width: 390, height: 844 }, }); this.page = await this.context.newPage(); } } } // 使用示例 // const config = require('./playwright_config')[platform]; // const uploader = new VideoUploader(platform, config); // await uploader.init(); // await uploader.loadState('./auth/douyin.json'); // const result = await uploader.upload('./video.mp4', { title: '标题', description: '描述' }); // await uploader.close(); ``` ```python # upload_py.py - Python 版本(可选) from playwright.sync_api import sync_playwright import time def upload_video(platform, video_path, title, description): with sync_playwright() as p: browser = p.chromium.launch(headless=False) context = browser.new_context( user_agent='Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X)', viewport={'width': 390, 'height': 844} ) page = context.new_page() # 登录 page.goto(f'https://{platform}.com/upload') page.wait_for_timeout(5000) # 上传文件 page.set_input_files('input[type="file"]', video_path) page.wait_for_timeout(10000) # 填写信息 page.fill('input[placeholder="标题"]', title) page.fill('textarea[placeholder="简介"]', description) # 发布 page.click('button:has-text("发布")]') page.wait_for_timeout(5000) browser.close() if __name__ == '__main__': upload_video('douyin', './test.mp4', '测试标题', '测试描述') ```