微信公众号运营工具集(WeChatMP Suite)
本技能是一个 微信公众号运营工具集,包含发文、草稿管理、评论管理、数据统计等多个脚本,支持多公众号同步。
本技能由极速数据提供:https://www.jisuapi.com
信息反馈:请发邮件至 liupandeng@jisuapi.com
官方文档:https://developers.weixin.qq.com/doc/subscription/api/
现阶段能力(按模块)
- 发文到草稿(publish.py)
- Markdown → HTML(本地转换)
- HTML 美化(
css/css_path/style_url抓取<style>内联样式) - 自动生成封面图(本地生成 PNG)并上传获取
thumb_media_id - 草稿箱发布:
/cgi-bin/draft/add
- 草稿管理(drafts.py)
- 批量获取草稿、获取详情、删除草稿
- 拉取源公众号草稿并同步到其它公众号(clone)
- 评论管理(comments.py)
- 查询评论、开启/关闭评论、精选/取消精选、回复/删除回复、删除评论
- 数据统计(stats.py)
- 常用 datacube 接口:群发、阅读、分享、用户增减等
安全说明:涉及本地文件读取的参数(例如
md_path、html_path、css_path、cover_path)默认只允许当前工作目录及子目录下的相对路径,禁止绝对路径与..目录穿越。
依赖安装
pip install requests markdown pillow
环境变量配置
单公众号(默认)
# Windows PowerShell
$env:WECHAT_MP_APP_ID="你的AppID"
$env:WECHAT_MP_APP_SECRET="你的AppSecret"
$env:WECHAT_MP_DEFAULT_AUTHOR="作者名(可选)"
多公众号同步(推荐)
用 WECHAT_MP_ACCOUNTS 传 JSON 数组:
# Windows PowerShell
$env:WECHAT_MP_ACCOUNTS='[
{"name":"公众号A","appid":"xxx","secret":"yyy"},
{"name":"公众号B","appid":"xxx2","secret":"yyy2"}
]'
脚本路径
脚本文件:
skills/wechatmp/publish.py(发文到草稿)skills/wechatmp/drafts.py(草稿管理)skills/wechatmp/comments.py(评论管理)skills/wechatmp/stats.py(数据统计)
使用方式
按脚本分别调用(每个脚本都有自己的子命令)。
发文到草稿(publish.py)
Windows(PowerShell)强烈推荐:用 @file 方式传 JSON
在 Windows PowerShell 下,直接把 JSON 写在命令行里很容易遇到:
- 中文变成
????(管道/控制台编码导致不可逆丢字) - JSON 转义/引号问题(导致解析失败)
推荐把请求参数保存成一个 UTF-8 的 JSON 文件,然后用 @文件路径 传入(脚本会读取该文件内容作为请求体):
# 1) 保存请求 JSON(注意用 UTF-8 保存)
# out\req.json 示例:
# {
# "title": "标题",
# "digest": "摘要",
# "html_path": "article.html",
# "debug_html_out": "out\\debug-final.html",
# "dry_run": 1
# }
# 2) 用 @file 模式执行(最稳)
python skills\wechatmp\publish.py draft @out\req.json
0) 直接从网络地址抓取 HTML(html_url)
当你不想先手动下载网页时,可以直接传 html_url。脚本会下载网页 HTML,自动提取 <body> 作为正文,并把 <head><style> 合并后做 inline(可配合 inline_css/strip_class)。
对于 mp.weixin.qq.com 链接,默认会优先提取页面里的 id="js_content" 正文(可显著降低体积,减少 45002 content size out of limit)。
python skills\wechatmp\publish.py draft @out\req.json
out\req.json 示例:
{
"title": "标题",
"html_url": "https://mp.weixin.qq.com/s/IRNhW6etkGIBkCQ7g5Y7HQ",
"debug_html_out": "out\\debug-final.html",
"dry_run": 1
}
如果目标页面需要登录态,可设置环境变量 WECHAT_MP_COOKIE,或在请求里传 html_url_cookie。
如果页面体积较大,可能需要传 html_url_max_bytes 提高下载上限(默认 2.5MB),例如 8000000。
0.1) 正文提取(公众号 / 网易 / 新浪)
当你使用 html_url 或 html_path 输入网页时,可以通过 content_selector 精确提取正文区域(取该节点的 innerHTML):
- 公众号文章(通常直接用 body):
- 可不传
content_selector,默认提取<body>(如果你有明确容器,也可手动指定)
- 可不传
- 网易文章:
- 常用:
"content_selector": "div.post_body"
- 常用:
- 新浪文章:
- 常用:
"content_selector": "div.article"
- 常用:
示例(网易):
{
"title": "标题",
"html_url": "https://www.163.com/tech/article/KOAS9OQJ00098IEO.html?clickfrom=w_tech",
"content_selector": "div.post_body",
"dry_run": 1,
"debug_html_out": "out/debug-163.html"
}
示例(新浪):
{
"title": "标题",
"html_url": "https://k.sina.com.cn/article_1644114654_61ff32de0200249su.html?from=news&subch=onews",
"content_selector": "div.article",
"dry_run": 1,
"debug_html_out": "out/debug-sina.html"
}
补充:
content_selector依赖beautifulsoup4(pip install beautifulsoup4)auto_content_selector默认开启时,会按常见站点尝试默认 selector(网易div.post_body、新浪div.article)
1) Markdown 文件 → HTML → 草稿
python3 skills/wechatmp/publish.py draft '{
"title": "我的文章标题",
"digest": "一句话摘要(建议填写)",
"md_path": "article.md"
}'
2) 传入 Markdown 内容(不落盘)
python3 skills/wechatmp/publish.py draft '{
"title": "标题",
"md_content": "# Hello\n\n正文…"
}'
3) 美化样式:直接传 css 或从网页抓样式
python3 skills/wechatmp/publish.py draft '{
"title": "标题",
"md_path": "article.md",
"css": "h1{color:#1677ff} .wechatmp-article{font-size:16px}",
"style_url": "https://example.com"
}'
说明:
style_url仅抓取页面中的<style>...</style>,不会自动拉取外链 CSS(轻量实现,避免过度复杂)。
4) 封面图:自动生成 / 指定本地 / 指定 base64 / 指定 URL
- 默认:若不传封面,脚本会自动生成
out/wechatmp-cover.png - 指定本地封面:
python3 skills/wechatmp/publish.py draft '{
"title": "标题",
"md_path": "article.md",
"cover_path": "assets/cover.png"
}'
- 直接传 base64:
python3 skills/wechatmp/publish.py draft '{
"title": "标题",
"md_path": "article.md",
"cover_base64": "<base64_string>",
"cover_out": "out/cover.png"
}'
- 指定封面 URL(会下载后上传永久素材):
python3 skills/wechatmp/publish.py draft '{
"title": "标题",
"md_path": "article.md",
"cover_url": "https://example.com/cover.jpg",
"cover_out": "out/cover_from_url.jpg"
}'
- 若未传封面参数,且你提供的
html_url / html_path含<meta property="og:image" ...>,默认会自动把og:image这张图当封面。- 可关闭:
"auto_cover_from_og_image": 0
- 可关闭:
请求参数(draft)
| 字段名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| title | string | 是 | 文章标题 |
| digest | string | 否 | 摘要 |
| author | string | 否 | 作者,不传用 WECHAT_MP_DEFAULT_AUTHOR |
| content_source_url | string | 否 | 原文链接 |
| md_path / md_content | string | 二选一 | Markdown 文件路径/内容 |
| html_path / html_content | string | 二选一 | 直接提供 HTML(优先于 Markdown) |
| content_selector | string | 否 | 从输入 HTML 中按 CSS selector 精确抽取正文 innerHTML(例如 div.post_body)。需要 pip install beautifulsoup4 |
| auto_content_selector | int | 否 | 默认 1:未传 content_selector 时自动按站点尝试(网易 div.post_body、新浪 div.article) |
| css | string | 否 | 额外 CSS(合并到 <style>) |
| css_path | string | 否 | 本地 CSS 文件路径(.css) |
| style_url | string | 否 | 从网页抓取 <style> 内联样式并合并 |
| cover_path | string | 否 | 本地封面图路径 |
| cover_base64 | string | 否 | 直接传封面 base64(会写到 cover_out) |
| cover_url | string | 否 | 封面图片 URL(会下载后上传永久素材) |
| cover_url_cookie | string | 否 | 下载 cover_url 用的 Cookie(可不传,默认用 WECHAT_MP_COOKIE) |
| cover_url_max_bytes | int | 否 | 下载封面图片最大字节数,默认 5000000 |
| cover_out | string | 否 | 封面输出路径(相对路径),默认 out/wechatmp-cover.png |
| auto_cover_from_og_image | int | 否 | 从页面 meta og:image 自动作为封面(默认 1) |
| need_open_comment | int | 否 | 是否打开评论(0/1),默认 0 |
| only_fans_can_comment | int | 否 | 是否仅粉丝可评(0/1),默认 0 |
| show_cover_pic | int | 否 | 正文是否显示封面(0/1),默认 1 |
| content_max_bytes | int | 否 | 发布前内容最大字节数限制(>0 生效,超限直接返回 content_too_large) |
返回结果
成功时会返回每个公众号创建的草稿 draft_media_id(以及封面 thumb_media_id),例如:
{
"ok": true,
"title": "我的文章标题",
"cover": "out/wechatmp-cover.png",
"accounts": [
{"account":"公众号A","ok":true,"draft_media_id":"xxx","thumb_media_id":"yyy"}
]
}
评论管理(comments.py)
脚本文件:skills/wechatmp/comments.py
说明:评论接口通常需要
msg_data_id与index等字段(来自图文消息数据)。你可以先在公众号后台或其它流程中拿到这些 id。
查询评论列表(list)
python3 skills/wechatmp/comments.py list '{
"msg_data_id": 123,
"index": 0,
"begin": 0,
"count": 20,
"type": 0
}'
开启/关闭评论(open/close)
python3 skills/wechatmp/comments.py open '{"msg_data_id":123,"index":0}'
python3 skills/wechatmp/comments.py close '{"msg_data_id":123,"index":0}'
精选/取消精选(mark/unmark)
python3 skills/wechatmp/comments.py mark '{"msg_data_id":123,"index":0,"user_comment_id":1}'
python3 skills/wechatmp/comments.py unmark '{"msg_data_id":123,"index":0,"user_comment_id":1}'
回复/删除回复(reply_add/reply_delete)
python3 skills/wechatmp/comments.py reply_add '{
"msg_data_id": 123,
"index": 0,
"user_comment_id": 1,
"content": "谢谢关注~"
}'
python3 skills/wechatmp/comments.py reply_delete '{"msg_data_id":123,"index":0,"user_comment_id":1}'
删除评论(delete)
python3 skills/wechatmp/comments.py delete '{"msg_data_id":123,"index":0,"user_comment_id":1}'
数据统计(stats.py)
脚本文件:skills/wechatmp/stats.py
支持常用 datacube 统计接口(均需传 begin_date / end_date,格式 YYYY-MM-DD):
articlesummary→getarticlesummaryarticletotal→getarticletotaluserread→getuserreadusershare→getusershareusercumulate→getusercumulate
示例:
python3 skills/wechatmp/stats.py articlesummary '{"begin_date":"2026-03-01","end_date":"2026-03-01"}'
python3 skills/wechatmp/stats.py userread '{"begin_date":"2026-03-01","end_date":"2026-03-07"}'
草稿管理(drafts.py)
脚本文件:skills/wechatmp/drafts.py
批量获取草稿列表(list)
python3 skills/wechatmp/drafts.py list '{"offset":0,"count":20,"no_content":1}'
获取草稿详情(get)
python3 skills/wechatmp/drafts.py get '{"media_id":"xxx"}'
删除草稿(delete)
python3 skills/wechatmp/drafts.py delete '{"media_id":"xxx"}'
拉取草稿并同步到其它公众号(clone)
适合“把 A 公众号已在草稿箱的内容,复制到 B/C 公众号草稿箱”场景。
脚本会先读取源草稿,再把每篇文章的thumb_url下载并上传到目标公众号,最后调用目标号draft/add。
python3 skills/wechatmp/drafts.py clone '{
"media_id": "SOURCE_DRAFT_MEDIA_ID",
"source_account": "公众号A",
"target_accounts": ["公众号B","公众号C"]
}'
参数说明:
media_id:源草稿 media_id(必填)source_account:源公众号名称(多账号时必填;单账号可省略)target_accounts:目标公众号名称数组(可省略;省略时默认同步到除源号外所有账号)cover_url_cookie:下载封面图(thumb_url)时使用的 Cookie(可选)cover_url_max_bytes:下载封面图最大字节数,默认5000000
后续规划
后续还可以继续在同目录扩展:
materials.py:素材管理(图片/视频等)publish.py:发布相关扩展(已实现 draft;后续可加 submit/status 等)
