利用 Python 从微信公众号文章中提取元数据与正文内容。适用于用户需要解析微信文章链接(mp.weixin.qq.com)、提取文章信息(标题、作者、正文、发布时间、封面图),或将微信文章转换为结构化数据的情景。
使用 Python 提取微信公众号文章的标题、作者、内容、发布时间等元数据。
相比 JavaScript 版本,Python 版本有以下优势:
bash
pip install beautifulsoup4 requests lxml
或使用国内镜像加速:
bash
pip install beautifulsoup4 requests lxml -i https://pypi.tuna.tsinghua.edu.cn/simple
bash
python3 scripts/wechat_extractor.py <微信文章 URL>
bash
extractor = WeChatArticleExtractor()
result = extractor.extract(https://mp.weixin.qq.com/s/xxx)
if result[done]:
print(标题:, result[data][msg_title])
print(作者:, result[data][msg_author])
print(内容:, result[data][msg_content][:500])
json
{
done: true,
code: 0,
data: {
// 文章信息
msg_title: 文章标题,
msg_desc: 文章摘要,
msg_content:
// URL 参数
msg_mid: mid 参数,
msg_idx: idx 参数,
msg_sn: sn 参数,
msgbiz: _biz 参数,
// 公众号信息
account_name: 公众号名称,
account_alias: 微信号,
account_id: 原始 ID,
account_description: 功能介绍,
account_avatar: 头像 URL,
// 版权信息
msghascopyright: true
}
}
json
{
done: false,
code: 1002,
msg: 请求超时
}
| 错误码 | 说明 | 解决方案 |
|---|---|---|
| 1001 | 无法获取文章信息 | 检查 URL 是否正确 |
| 1002 |
✅ 适用场景:
❌ 不适用场景:
python
urls = [
https://mp.weixin.qq.com/s/xxx1,
https://mp.weixin.qq.com/s/xxx2,
https://mp.weixin.qq.com/s/xxx3,
]
extractor = WeChatArticleExtractor(timeout=30)
for url in urls:
result = extractor.extract(url)
if result[done]:
print(f✅ {result[data][msg_title]})
else:
print(f❌ {url}: {result[msg]})
# 避免请求过快
import time
time.sleep(1)
python
import json
result = extractor.extract(url)
with open(article.json, w, encoding=utf-8) as f:
json.dump(result, f, ensure_ascii=False, indent=2)
python
from bs4 import BeautifulSoup
result = extractor.extract(url)
htmlcontent = result[data][msgcontent]
python
extractor = WeChatArticleExtractor(timeout=60)
extractor.session.headers.update({
User-Agent: 自定义 User-Agent
})
python
extractor = WeChatArticleExtractor(timeout=30)
extractor.session.proxies.update({
http: http://127.0.0.1:7890,
https: http://127.0.0.1:7890
})
| 特性 | Python 版本 | JavaScript 版本 |
|---|---|---|
| 依赖 | pip (bs4, requests) | npm (cheerio, request-promise) |
| 安装速度 |
正在提取文章:https://mp.weixin.qq.com/s/xN1H5s66ruXY9s8aOd4Rcg
📰 文章标题:4B 参数实现理解、推理、生成、编辑一体化!InternVL-U 重磅开源
👤 作者:书生 Intern
📢 公众号:书生 Intern
⏰ 发布时间:2026/03/14 10:30:00
📝 文章摘要:重新定义统一多模态模型的 效率 - 性能 边界。
🖼️ 封面图:https://mmbiz.qpic.cn/mmbiz_jpg/...
📄 文章类型:post
🔗 文章链接:https://mp.weixin.qq.com/s/xN1H5s66ruXY9s8aOd4Rcg
📊 公众号信息:
- 名称:书生 Intern
- 微信号:未设置
- 原始 ID: 未设置
- 功能介绍:未设置
📝 文章内容长度:129134 字符
💾 详细数据已保存到:/tmp/wechat_article.json
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 mp-weixin-1776191525 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 mp-weixin-1776191525 技能
skillhub install mp-weixin-1776191525
文件大小: 8.22 KB | 发布时间: 2026-4-17 15:27