Loading...
Loading...
通用自媒体文章自动发布工具。支持百家号、搜狐号、知乎三个平台的自动化发布流程。使用Playwright MCP工具实现平台导航和发布页面进入,支持通过storageState管理Cookie实现账号切换。当用户需要发布文章到自媒体平台、打开平台发布页面、切换自媒体账号时使用此skill。工作流程:1)检查并打开指定平台 2)验证登录状态 3)自动关闭广告弹窗 4)导航到发布文章页面 5)收集发布信息 6)自动填写并发布。
npx skill4agent add aaaaqwq/claude-code-skills media-auto-publisher用户: 打开百家号发布页面
用户: 帮我进入知乎写文章
用户: 切换到搜狐号发布文章
用户: 帮我发布文章到百家号| 平台代码 | 平台名称 | 发布页面URL | 自动化状态 |
|---|---|---|---|
| 百家号 | https://baijiahao.baidu.com/builder/rc/edit?type=news | ✅ 完全支持 |
| 搜狐号 | https://mp.sohu.com/api/author/article/new | ✅ 完全支持 |
| 知乎 | https://zhuanlan.zhihu.com/write | ✅ 支持Cookie登录 |
重大更新: 使用 Playwright MCP 后,知乎可以通过管理登录态,不再需要调试端口!storageState
mcp__plugin_playwright_playwright__browser_navigate| 字段 | 说明 | 示例 |
|---|---|---|
| 文章标题 | 文章的标题 | "如何用Python自动化发布文章" |
| 正文内容 | Markdown格式的文章内容 | "# 引言\n这是正文..." |
| 字段 | 说明 | 适用平台 |
|---|---|---|
| 封面图片 | 本地图片路径或图片URL | 全平台 |
| 文章摘要 | 文章的简短摘要 | 百家号/搜狐号 |
| 字段 | 说明 | 适用平台 |
|---|---|---|
| 文章话题/标签 | 相关话题标签,多个用逗号分隔 | 全平台 |
| 投稿至问题 | 将文章投稿到的知乎问题 | 知乎 |
| 分类/栏目 | 文章所属分类 | 百家号/搜狐号 |
| 字段 | 说明 | 适用平台 |
|---|---|---|
| 创作声明 | 原创声明/转载声明 | 全平台 |
| 可见性设置 | 公开/仅自己可见/定时发布 | 全平台 |
| 专栏 | 发布到指定专栏 | 知乎 |
python
# 步骤1:导航到百家号首页
mcp__plugin_playwright_playwright__browser_navigate(url="https://baijiahao.baidu.com/")
# 步骤2:检查登录状态
mcp__plugin_playwright_playwright__browser_snapshot()
# 步骤3:关闭百家号特有弹窗
mcp__plugin_playwright_playwright__browser_run_code(code='''
async (page) => {
const closeSelectors = ['下一步', '立即体验', '我知道了', '知道了'];
for (const selector of closeSelectors) {
try {
const element = await page.getByText(selector).first();
if (await element.isVisible()) {
await element.click();
await page.waitForTimeout(500);
}
} catch (e) {}
}
await page.evaluate(() => {
document.querySelectorAll('dialog, [role="dialog"], .tooltip').forEach(el => el.remove());
});
return { success: true };
}
''')
# 步骤4:进入发布页面(直接导航)
mcp__plugin_playwright_playwright__browser_navigate(url="https://baijiahao.baidu.com/builder/rc/edit?type=news")
# 步骤5:填写文章标题
mcp__plugin_playwright_playwright__browser_run_code(code='''
async (page) => {
const titleInput = await page.locator('input[placeholder*="请输入标题"]').first();
await titleInput.fill('{文章标题}');
return { success: true };
}
''')
# 步骤6:填写正文内容(百家号在iframe中)
mcp__plugin_playwright_playwright__browser_run_code(code='''
async (page) => {
const frame = page.frame({ url: /editor/ }) || page.mainFrame();
await frame.locator('[contenteditable="true"], .editor-body').first().fill('{正文内容}');
return { success: true };
}
''')
# 步骤7:上传封面图(如果提供)
if 封面图片:
mcp__plugin_playwright_playwright__browser_click(ref="三图模式")
for 图片路径 in 封面图片列表:
mcp__plugin_playwright_playwright__browser_file_upload(paths=["{图片路径}"])
# 步骤8:填写摘要(如果提供)
if 文章摘要:
mcp__plugin_playwright_playwright__browser_run_code(code='''
async (page) => {
const summaryInput = await page.locator('textarea[placeholder*="摘要"]').first();
await summaryInput.fill('{文章摘要}');
return { success: true };
}
''')
# 步骤9:选择分类(如果提供)
if 文章分类:
mcp__plugin_playwright_playwright__browser_click(ref="分类选择器")
mcp__plugin_playwright_playwright__browser_click(ref="{分类}按钮")
# 步骤10:发布前确认
mcp__plugin_playwright_playwright__browser_take_screenshot(path="百家号发布前预览.png")
# 步骤11:点击发布
mcp__plugin_playwright_playwright__browser_click(ref="发布按钮")
# 步骤12:验证发布成功
mcp__plugin_playwright_playwright__browser_wait_for(element="发布成功提示", timeout=10000)python
# 步骤1:导航到搜狐号发布页
mcp__plugin_playwright_playwright__browser_navigate(url="https://mp.sohu.com/api/author/article/new")
# 步骤2:检查登录状态
mcp__plugin_playwright_playwright__browser_snapshot()
# 步骤3:关闭弹窗(如有)
mcp__plugin_playwright_playwright__browser_run_code(code='''
async (page) => {
const closeSelectors = ['我知道了', '知道了', '关闭'];
for (const selector of closeSelectors) {
try {
await page.getByText(selector).first().click();
await page.waitForTimeout(300);
} catch (e) {}
}
return { success: true };
}
''')
# 步骤4:填写文章标题
mcp__plugin_playwright_playwright__browser_run_code(code='''
async (page) => {
const titleInput = await page.locator('input[name="title"], input[placeholder*="标题"]').first();
await titleInput.fill('{文章标题}');
return { success: true };
}
''')
# 步骤5:填写正文内容
mcp__plugin_playwright_playwright__browser_run_code(code='''
async (page) => {
const editor = await page.locator('#editor, .editor-content, [contenteditable="true"]').first();
await editor.fill('{正文内容}');
return { success: true };
}
''')
# 步骤6:上传封面图(如果提供)
if 封面图片:
mcp__plugin_playwright_playwright__browser_click(ref="封面上传按钮")
mcp__plugin_playwright_playwright__browser_file_upload(paths=["{封面图片路径}"])
# 步骤7:填写摘要(如果提供)
if 文章摘要:
mcp__plugin_playwright_playwright__browser_run_code(code='''
async (page) => {
const summaryInput = await page.locator('textarea[name="summary"]').first();
await summaryInput.fill('{文章摘要}');
return { success: true };
}
''')
# 步骤8:选择栏目(如果提供)
if 文章栏目:
mcp__plugin_playwright_playwright__browser_click(ref="栏目选择器")
mcp__plugin_playwright_playwright__browser_click(ref="{栏目}按钮")
# 步骤9:设置原创声明(如果需要)
if 创作声明 == "原创":
mcp__plugin_playwright_playwright__browser_click(ref="原创声明开关")
# 步骤10:发布前确认
mcp__plugin_playwright_playwright__browser_take_screenshot(path="搜狐号发布前预览.png")
# 步骤11:点击发布
mcp__plugin_playwright_playwright__browser_click(ref="发布按钮")
# 步骤12:验证发布成功
mcp__plugin_playwright_playwright__browser_wait_for(element="发布成功", timeout=10000)python
# 步骤1:导航到知乎写文章页面
mcp__plugin_playwright_playwright__browser_navigate(url="https://zhuanlan.zhihu.com/write")
# 步骤2:检查登录状态
mcp__plugin_playwright_playwright__browser_snapshot()
# 步骤3:关闭弹窗(如有)
mcp__plugin_playwright_playwright__browser_run_code(code='''
async (page) => {
const closeSelectors = ['跳过', '以后再说', '不再提示'];
for (const selector of closeSelectors) {
try {
await page.getByText(selector).first().click();
await page.waitForTimeout(300);
} catch (e) {}
}
return { success: true };
}
''')
# 步骤4:填写文章标题
mcp__plugin_playwright_playwright__browser_run_code(code='''
async (page) => {
const titleInput = await page.locator('.WriteIndex-titleInput, input[placeholder*="请输入标题"]').first();
await titleInput.fill('{文章标题}');
return { success: true };
}
''')
# 步骤5:填写正文内容
mcp__plugin_playwright_playwright__browser_run_code(code='''
async (page) => {
const editor = await page.locator('.PublicDraftEditor-content, [contenteditable="true"]').first();
await editor.fill('{正文内容}');
return { success: true };
}
''')
# 步骤6:上传封面图(如果提供)
if 封面图片:
mcp__plugin_playwright_playwright__browser_run_code(code='''
async (page) => {
await page.locator('.UploadCoverButton, [class*="upload"]').first().click();
return { success: true };
}
''')
mcp__plugin_playwright_playwright__browser_file_upload(paths=["{封面图片路径}"])
# 步骤7:添加话题标签(如果提供)
if 文章话题:
for 话题 in 文章话题列表:
mcp__plugin_playwright_playwright__browser_run_code(code='''
async (page) => {
await page.locator('text=添加话题, .TopicEditor-input').first().click();
await page.locator('input[placeholder*="搜索话题"]').fill('{话题}');
await page.waitForTimeout(500);
await page.locator('.TopicItem, [role="option"]').first().click();
return { success: true };
}
''')
# 步骤8:投稿至问题(如果提供)
if 投稿至问题:
mcp__plugin_playwright_playwright__browser_run_code(code='''
async (page) => {
await page.locator('text=投稿至问题, .PublishToQuestion').first().click();
await page.locator('input[placeholder*="搜索问题"]').fill('{问题标题}');
await page.waitForTimeout(500);
await page.locator('.QuestionItem, [role="option"]').first().click();
return { success: true };
}
''')
# 步骤9:设置创作声明(如果需要)
if 创作声明:
mcp__plugin_playwright_playwright__browser_run_code(code='''
async (page) => {
await page.locator('button[class*="setting"], text=设置').first().click();
await page.locator('text={声明类型}').first().click();
return { success: true };
}
''')
# 步骤10:选择专栏(如果提供)
if 发布专栏:
mcp__plugin_playwright_playwright__browser_run_code(code='''
async (page) => {
await page.locator('text=选择专栏, .ColumnSelector').first().click();
await page.locator('text={专栏名称}').first().click();
return { success: true };
}
''')
# 步骤11:发布前确认
mcp__plugin_playwright_playwright__browser_take_screenshot(path="知乎发布前预览.png")
# 步骤12:点击发布
mcp__plugin_playwright_playwright__browser_run_code(code='''
async (page) => {
const publishBtn = await page.getByText('发布').first();
await publishBtn.click();
return { success: true };
}
''')
# 步骤13:验证发布成功
mcp__plugin_playwright_playwright__browser_wait_for(element="发布成功", timeout=10000)| 字段 | 百家号选择器 | 搜狐号选择器 | 知乎选择器 |
|---|---|---|---|
| 标题输入框 | input[placeholder*="请输入标题"] | input[name="title"] | .WriteIndex-titleInput |
| 正文编辑器 | iframe .editor-body | #editor | .PublicDraftEditor-content |
| 封面上传 | .upload-cover-btn | .cover-upload | .UploadCoverButton |
| 摘要输入框 | textarea[placeholder*="摘要"] | textarea[name="summary"] | ❌ 不支持 |
| 分类选择器 | .category-select | .column-select | ❌ 用话题替代 |
| 话题输入框 | ❌ 不支持 | .tag-input | .TopicEditor-input |
| 投稿问题 | ❌ 不支持 | ❌ 不支持 | .PublishToQuestion |
| 发布按钮 | button:has-text("发布") | button.publish-btn | button:has-text("发布") |
// 1. 手动登录一次平台
await page.goto("https://www.zhihu.com")
// ... 用户手动登录 ...
// 2. 保存登录状态
await context.storageState({ path: 'zhihu_account1.json' })// 加载已保存的登录态
const context = await browser.newContext({
storageState: 'zhihu_account1.json'
})python scripts/media_publisher.py baijiahao
python scripts/media_publisher.py open baijiahaopython scripts/media_publisher.py info baijiahaopython scripts/media_publisher.py workflow baijiahao
python scripts/media_publisher.py workflow sohu --output workflow.json --format jsonpython scripts/media_publisher.py check-login baijiahao
python scripts/media_publisher.py check-login sohu --snapshot snapshot.txtpython scripts/media_publisher.py detect-popup baijiahao
python scripts/media_publisher.py detect-popup sohu --snapshot snapshot.txtpython scripts/cookie_manager.py save zhihu --account "账号1"
python scripts/cookie_manager.py list
python scripts/cookie_manager.py switch zhihu "账号1"python
mcp__plugin_playwright_playwright__browser_run_code(code='''
async (page) => {
const closeSelectors = ['下一步', '立即体验', '我知道了', '知道了'];
for (const selector of closeSelectors) {
try {
const element = await page.getByText(selector).first();
if (await element.isVisible()) {
await element.click();
await page.waitForTimeout(500);
}
} catch (e) {}
}
await page.evaluate(() => {
document.querySelectorAll('dialog, [role="dialog"], .tooltip').forEach(el => el.remove());
});
return { success: true };
}
''')| 平台 | 关闭按钮关键词 |
|---|---|
| 百家号 | 我知道了、下一步、立即体验、关闭、× |
| 搜狐号 | 我知道了、知道了、关闭、× |
| 知乎 | 跳过、以后再说、不再提示、关闭 |
| 功能 | Chrome DevTools MCP | Playwright MCP |
|---|---|---|
| 导航 | navigate_page | browser_navigate |
| 快照 | take_snapshot | browser_snapshot |
| 点击 | click | browser_click |
| 填写 | fill | browser_fill_form / browser_type |
| 截图 | - | browser_take_screenshot |
| 等待 | - | browser_wait_for |
| 执行代码 | - | browser_run_code |
| 文件上传 | - | browser_file_upload |
await browser.newContext({
storageState: 'zhihu_cookies.json'
})| 问题 | 解决方案 |
|---|---|
| 未登录 | 提示用户先登录并保存 storageState |
| Cookie失效 | 使用cookie_manager.py重新保存登录状态 |
| 弹窗无法关闭 | 手动截图,分析弹窗结构后更新关闭规则 |
| 页面加载超时 | 使用browser_wait_for增加等待时间 |
| 知乎反爬 | Playwright 的 storageState 可有效绕过检测 |
| 填写失败 | 使用browser_run_code直接执行JavaScript |
| 特性 | Chrome DevTools | Playwright |
|---|---|---|
| 知乎登录 | 需要调试端口 | storageState 支持 |
| 多浏览器 | 仅 Chrome | Chrome/Firefox/Safari |
| Cookie管理 | 手动导入 | storageState() API |
| 反爬检测 | 自动化特征明显 | 可伪装真实浏览器 |
| 跨平台 | 依赖 Chrome | 多引擎支持 |
| 文件上传 | 需要特殊处理 | browser_file_upload |
async (page) => {
await page.setExtraHTTPHeaders({
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
});
await page.addInitScript(() => {
Object.defineProperty(navigator, 'webdriver', {
get: () => false
});
});
await page.goto('https://zhuanlan.zhihu.com/write');
}python scripts/cookie_manager.py list
python scripts/cookie_manager.py save zhihu --account "主账号"
python scripts/cookie_manager.py save zhihu --account "小号"