Skip to main content

A Python tool for fetching web articles and converting them to Markdown format

Project description

WeSpy

PyPI version Python Support License: MIT

WeSpy 是一个用于获取微信公众号文章并转换为 Markdown 格式的 Python 工具,支持图片防盗链处理和多种输出格式。

特性

  • 🚀 智能文章提取:自动识别文章标题、作者、发布时间和正文内容
  • 📱 微信公众号支持:专门优化微信公众号文章的提取
  • 🖼️ 图片防盗链处理:自动处理图片防盗链问题,确保图片正常显示
  • 📝 灵活输出配置:默认只输出 Markdown,可选择 HTML 和 JSON 格式
  • 🌐 通用网页支持:支持大多数网站的文章提取
  • 🎯 命令行友好:提供简单易用的命令行界面
  • 📂 批量处理:支持批量处理多个文章链接

安装

使用 pip 安装(推荐)

pip install wespy

从源码安装

git clone https://github.com/tianchang/wespy.git
cd wespy
pip install -e .

快速开始

命令行使用

# 获取微信公众号文章(默认只输出 Markdown)
wespy "https://mp.weixin.qq.com/s/xxxxx"

# 指定输出目录
wespy "https://mp.weixin.qq.com/s/xxxxx" -o /path/to/output

# 输出 Markdown + HTML 格式
wespy "https://example.com/article" --html

# 输出 Markdown + JSON 格式
wespy "https://example.com/article" --json

# 输出所有格式(HTML + JSON + Markdown)
wespy "https://example.com/article" --all

# 显示详细信息
wespy "https://example.com/article" -v

交互式使用

如果不提供任何参数,程序会进入交互模式:

wespy

然后根据提示输入文章 URL、输出目录和输出格式选择:

  1. 仅 Markdown(默认)
  2. Markdown + HTML
  3. Markdown + JSON
  4. 全部格式(HTML + JSON + Markdown)

Python API 使用

from wespy import ArticleFetcher

# 创建文章获取器实例
fetcher = ArticleFetcher()

# 获取文章(默认只输出 Markdown)
article_info = fetcher.fetch_article(
    url="https://mp.weixin.qq.com/s/xxxxx",
    output_dir="articles"
)

# 获取文章并指定输出格式
article_info = fetcher.fetch_article(
    url="https://mp.weixin.qq.com/s/xxxxx",
    output_dir="articles",
    save_html=True,      # 同时保存HTML文件
    save_json=True,      # 同时保存JSON文件
    save_markdown=True   # 保存Markdown文件(默认为True)
)

if article_info:
    print(f"标题: {article_info['title']}")
    print(f"作者: {article_info['author']}")
    print(f"发布时间: {article_info['publish_time']}")

输出格式

WeSpy 默认只生成 Markdown 文件,但可以通过配置选项选择其他格式:

默认输出(仅 Markdown)

articles/
└── 文章标题_1627834567.md        # Markdown格式

可选格式

  • HTML 文件:原始 HTML 内容(使用 --html 选项)
  • JSON 文件:文章元数据信息(使用 --json 选项)
  • Markdown 文件:转换后的 Markdown 格式内容(默认生成)

全部格式输出示例

articles/
├── 文章标题_1627834567.html      # 原始HTML
├── 文章标题_1627834567.md        # Markdown格式
└── 文章标题_1627834567_info.json # 元数据信息

JSON 元数据格式

{
  "title": "文章标题",
  "author": "作者名称",
  "publish_time": "2023-07-30",
  "url": "https://example.com/article",
  "html_file": "文章标题_1627834567.html",
  "fetch_time": "2023-07-30 12:34:56"
}

支持的网站

完全支持

  • 微信公众号 (mp.weixin.qq.com)
  • 大部分基于标准 HTML 结构的博客和新闻网站

通用支持

WeSpy 使用智能算法尝试从以下元素中提取内容:

  • <article> 标签
  • 带有 contentarticle-contentpost-content 等 class 的元素
  • <main> 标签
  • 标准的 meta 标签信息

命令行选项

wespy [-h] [-o OUTPUT] [-v] [--html] [--json] [--all] url

获取文章内容并转换为Markdown

positional arguments:
  url                   文章URL

optional arguments:
  -h, --help            显示帮助信息
  -o OUTPUT, --output OUTPUT
                        输出目录 (默认: articles)
  -v, --verbose         显示详细信息
  --html                同时保存HTML文件
  --json                同时保存JSON信息文件
  --all                 保存所有格式文件 (HTML, JSON, Markdown)

输出格式选项说明

  • 默认行为:只生成 Markdown 文件
  • --html:生成 Markdown + HTML 文件
  • --json:生成 Markdown + JSON 文件
  • --all:生成所有格式文件(HTML + JSON + Markdown)

依赖要求

  • Python 3.6+
  • requests >= 2.20.0
  • beautifulsoup4 >= 4.9.0

开发

开发环境设置

git clone https://github.com/tianchang/wespy.git
cd wespy
pip install -e ".[dev]"

运行测试

python -m pytest tests/

代码格式化

black wespy/
flake8 wespy/

常见问题

Q: 为什么有些图片无法显示?

A: WeSpy 使用 images.weserv.nl 作为代理服务来解决图片防盗链问题。如果仍然无法显示,可能是原图片已被删除或网络问题。

Q: 支持哪些网站?

A: WeSpy 对微信公众号有特别优化,对大部分使用标准 HTML 结构的网站都有较好的支持。如果某个网站不支持,欢迎提交 issue。

Q: 如何批量处理文章?

A: 目前需要通过脚本调用 Python API 来实现批量处理,命令行版本暂不支持批量处理。

贡献

欢迎提交 issue 和 pull request!

  1. Fork 本仓库
  2. 创建特性分支 (git checkout -b feature/AmazingFeature)
  3. 提交更改 (git commit -m 'Add some AmazingFeature')
  4. 推送到分支 (git push origin feature/AmazingFeature)
  5. 开启 Pull Request

许可证

本项目使用 MIT 许可证。详见 LICENSE 文件。

更新日志

v0.1.2 (2025-01-01)

  • 改进输出格式配置:默认只输出 Markdown 文件
  • 新增命令行选项--html--json--all 用于控制输出格式
  • 优化交互模式:添加输出格式选择菜单
  • 更新 Python API:支持参数化输出格式控制

v0.1.0 (2023-07-30)

  • 初始版本发布
  • 支持微信公众号文章提取
  • 支持通用网页文章提取
  • 支持 HTML/JSON/Markdown 多格式输出
  • 图片防盗链处理
  • 命令行界面

联系方式


注意: 请遵守网站的 robots.txt 和使用条款,合理使用本工具。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wespy-0.1.3.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wespy-0.1.3-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file wespy-0.1.3.tar.gz.

File metadata

  • Download URL: wespy-0.1.3.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for wespy-0.1.3.tar.gz
Algorithm Hash digest
SHA256 2a296bc6d4704aea3c4ab216bd296c243262ff8b65f6f86eb9ba4a363e80c689
MD5 65a7d74d89d0de47822967afd7348621
BLAKE2b-256 6848d875de6241f487bb67de2ced35f502b4facbd9d3db72cf204faff1615a81

See more details on using hashes here.

File details

Details for the file wespy-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: wespy-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for wespy-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2a61a4d4603638d982f7462936ad568a4d496c01fbf19faf7e5a87974274b921
MD5 ac12d94d137725a4d0e853c90bd0b64a
BLAKE2b-256 e79c127109f71d72aa58c96b619ded37c97927f2927defa61e52fc4c99b311e4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page