使用 Python 提取微信公众号文章的标题、作者、内容、发布时间等元数据。
相比 JavaScript 版本,Python 版本有以下优势:
pip install beautifulsoup4 requests lxml 即可完成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
# 提取单篇文章
python3 scripts/wechat_extractor.py "https://mp.weixin.qq.com/s/xN1H5s66ruXY9s8aOd4Rcg"
# 在 Python 脚本中调用
from scripts.wechat_extractor import WeChatArticleExtractor
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": "<div>HTML 内容</div>",
"msg_cover": "封面图 URL",
"msg_author": "作者",
"msg_type": "post",
"msg_publish_time_str": "2026/03/14 10:30:00",
"msg_link": "文章链接",
// URL 参数
"msg_mid": "mid 参数",
"msg_idx": "idx 参数",
"msg_sn": "sn 参数",
"msg_biz": "__biz 参数",
// 公众号信息
"account_name": "公众号名称",
"account_alias": "微信号",
"account_id": "原始 ID",
"account_description": "功能介绍",
"account_avatar": "头像 URL",
// 版权信息
"msg_has_copyright": true
}
}
json
{
"done": false,
"code": 1002,
"msg": "请求超时"
}
错误码说明解决方案1001无法获取文章信息检查 URL 是否正确1002请求失败/超时检查网络连接,稍后重试2006需要验证码微信反爬机制,稍后重试或使用已登录会话2008系统出错查看错误详情,联系开发者
✅ 适用场景:
❌ 不适用场景:
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)
html_content = result['data']['msg_content']
# 转为纯文本
soup = BeautifulSoup(html_content, 'lxml')
text_content = soup.get_text(separator='\n', strip=True)
print(text_content)
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'
})
验证码问题:微信有反爬机制,频繁访问可能触发验证码,建议:
内容完整性:部分文章可能包含视频、音频等多媒体内容,HTML 内容中会保留引用链接
发布时间:微信文章的发布时间可能无法精确提取,部分文章只能获取大致日期
公众号信息:公众号详细信息(微信号、原始 ID 等)需要从页面 JavaScript 中提取,可能不完整
特性Python 版本JavaScript 版本依赖pip (bs4, requests)npm (cheerio, request-promise)安装速度快(<30 秒)慢(可能超时)代码量300 行600 行可维护性高中验证码处理使用微信 UA 绕过需要额外配置推荐度⭐⭐⭐⭐⭐⭐⭐⭐
text
正在提取文章: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
Python 版本 by bozoyan · 2026-03-14 · 专为 CoPaw 优化 📰✨