PSYnala/wechat-parser-mcp
3.2
If you are the rightful owner of wechat-parser-mcp and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.
A powerful local MCP server for parsing WeChat articles, offering comprehensive content extraction, batch processing, and content cleaning capabilities.
Tools
5
Resources
0
Prompts
0
微信文章解析MCP Server - 本地版
🚀 一个强大的微信文章解析MCP Server,支持本地运行,提供完整的文章内容提取、批量处理和内容清洗功能。
✨ 功能特性
🔍 核心功能
- 单篇解析: 快速解析微信文章标题、作者、发布时间等基本信息
- 深度提取: 获取完整文章正文、图片链接、视频链接
- 批量处理: 支持同时处理多个文章链接(最多10个)
- 内容清洗: 智能去除多余空白和格式字符
- 本地保存: 自动保存文章到本地JSON文件
🛠️ MCP工具
parse_wechat_article: 解析微信文章基本信息extract_full_content: 获取完整文章内容和媒体资源batch_parse_articles: 批量处理多篇文章clean_article_content: 清洗文章内容save_article_local: 保存文章到本地
📚 资源
file://articles/sample.json: 示例文章数据格式
🚀 快速开始
环境要求
- macOS 10.15+
- Python 3.8+
- Chrome浏览器
- 网络连接(首次运行需要下载ChromeDriver)
安装步骤
- 安装依赖
# 进入项目目录
cd /Users/nala/Desktop/zhuan/my-first-mcp-server
# 安装Python依赖(如果未安装)
uv sync
# 运行安装脚本(自动配置ChromeDriver)
chmod +x install_dependencies.sh
./install_dependencies.sh
- 启动服务
# 启动MCP Server
python3 server.py
# 或使用uv运行
uv run python server.py
使用示例
# 1. 解析文章基本信息
result = parse_wechat_article("https://mp.weixin.qq.com/s/example")
print(f"标题: {result['data']['title']}")
# 2. 获取完整内容
article = extract_full_content("https://mp.weixin.qq.com/s/example")
print(f"文章字数: {article['data']['word_count']}")
print(f"图片数量: {len(article['data']['images'])}")
# 3. 批量处理
urls = [
"https://mp.weixin.qq.com/s/article1",
"https://mp.weixin.qq.com/s/article2",
"https://mp.weixin.qq.com/s/article3"
]
results = batch_parse_articles(urls)
print(f"成功解析: {results['summary']['success_count']}/{len(urls)}")
# 4. 清洗内容
cleaned = clean_article_content(" 原始 文本 \n 内容 ")
print(f"压缩率: {cleaned['statistics']['compression_ratio']}")
# 5. 保存到本地
save_result = save_article_local(article['data'], "my_article")
print(f"保存路径: {save_result['file_path']}")
📁 项目结构
my-first-mcp-server/
├── wechat_parser.py # 核心解析模块
├── server.py # 启动脚本
├── install_dependencies.sh # 依赖安装脚本
├── pyproject.toml # 项目配置
├── articles/ # 文章保存目录
├── logs/ # 日志目录
├── cache/ # 缓存目录
└── README.md # 说明文档
🏗️ 技术架构
核心组件
- FastMCP: 基于MCP协议的服务框架
- Selenium: 浏览器自动化,处理动态加载内容
- BeautifulSoup: HTML解析,提取结构化数据
- SQLite: 本地数据存储(扩展功能)
技术特点
- 反爬虫策略: 随机延迟、User-Agent伪装
- 容错机制: 完善的异常处理和重试逻辑
- 性能优化: 智能等待、批量处理
- 数据模型: 完整的文章数据结构
🔧 配置说明
ChromeDriver配置
服务会自动管理ChromeDriver版本,首次运行时会下载匹配Chrome版本的驱动。
自定义配置
可以在wechat_parser.py中调整以下参数:
# 爬虫超时时间
timeout = 30
# 随机延迟范围
time.sleep(random.uniform(1, 3))
# 批量处理最大数量
if len(urls) > 10: # 最多10个
📋 API文档
parse_wechat_article(url: str)
解析微信文章基本信息
参数:
url: 微信文章链接
返回:
{
"success": true,
"data": {
"title": "文章标题",
"author": "作者",
"publish_time": "发布时间",
"url": "原文链接",
"summary": "文章摘要"
},
"processing_time": 2.34
}
extract_full_content(url: str)
获取完整文章内容和媒体资源
参数:
url: 微信文章链接
返回:
{
"success": true,
"data": {
"title": "文章标题",
"content": "文章正文",
"author": "作者",
"publish_time": "发布时间",
"url": "原文链接",
"images": ["图片链接列表"],
"videos": ["视频链接列表"],
"word_count": 1234
},
"processing_time": 3.45
}
batch_parse_articles(urls: List[str])
批量处理多个文章链接
参数:
urls: 文章链接列表(最多10个)
返回:
{
"success": true,
"results": [
{
"url": "链接1",
"success": true,
"data": {...}
}
],
"summary": {
"total": 3,
"success_count": 2,
"failed_count": 1,
"processing_time": 8.76
}
}
clean_article_content(raw_text: str)
清洗文章内容,去除多余空白
参数:
raw_text: 原始文章文本
返回:
{
"success": true,
"cleaned_text": "清洗后的文本",
"statistics": {
"original_length": 1000,
"cleaned_length": 850,
"word_count": 800,
"compression_ratio": 0.85
},
"processing_time": 0.12
}
save_article_local(article_data: dict, filename: str = None)
保存文章到本地文件
参数:
article_data: 文章数据字典filename: 保存文件名(可选)
返回:
{
"success": true,
"file_path": "articles/标题_时间戳.json",
"file_size": 1234,
"processing_time": 0.05
}
⚠️ 注意事项
法律合规
- 仅用于学习研究目的
- 遵守网站robots.txt协议
- 合理控制访问频率,避免对服务器造成压力
- 不用于商业用途或大规模抓取
技术限制
- 需要稳定的网络连接
- Chrome浏览器必须已安装
- 首次运行需要下载ChromeDriver(约100MB)
- 部分文章可能因反爬机制无法解析
性能建议
- 批量处理时建议间隔1秒以上
- 避免同时运行多个爬虫实例
- 定期清理缓存目录
- 监控ChromeDriver进程状态
🐛 故障排除
常见问题
-
ChromeDriver初始化失败
# 解决方案:重新运行安装脚本 ./install_dependencies.sh -
依赖模块缺失
# 解决方案:重新安装依赖 uv sync -
页面加载超时
- 检查网络连接
- 确认URL是否可访问
- 尝试使用代理
-
内容解析失败
- 确认是有效的微信公众号文章链接
- 检查页面是否需要登录
- 等待适当延迟时间
日志查看
# 查看错误日志
tail -f logs/error.log
# 查看所有日志
tail -f logs/*.log
🔄 后续开发
计划功能
- 支持更多自媒体平台
- 增加情感分析和关键词提取
- 实现分布式爬虫
- 添加GUI界面
- 支持数据库存储
贡献指南
欢迎提交Issue和Pull Request来改进项目!
版本: 1.0.0
作者: Nala
许可证: MIT
更新时间: 2024-12-10