Skip to main content

WeChat MP article parser - extract metadata and content from WeChat public account articles

Project description

wechat-article-parser

微信公众号文章解析器。传入一个公众号文章链接,自动抓取页面并提取公众号信息、文章元数据及正文的 Markdown 内容。

支持的文章类型:

  • 标准富文本文章
  • 转载式文章
  • 纯文本文章
  • 视频分享类文章
  • 小红书风格图片轮播文章
  • 全屏布局短文本文章

安装

pip install wechat-article-parser

本地开发安装:

git clone https://github.com/huanjuedadehen/wechat-article-parser.git
cd wechat-article-parser
pip install -e ".[dev]"

依赖项(会自动安装):

  • Python >= 3.10
  • httpx
  • beautifulsoup4
  • markdownify

使用方式

同步调用

from wechat_article_parser import parse

result = parse("https://mp.weixin.qq.com/s/xxxxx")
print(result.article_title)
print(result.article_markdown)

异步调用

from wechat_article_parser import parse_async

result = await parse_async("https://mp.weixin.qq.com/s/xxxxx")
print(result.article_title)
print(result.article_markdown)

可选参数

两个方法都支持以下可选参数:

  • timeout:请求超时时间,单位秒,默认 15
  • user_agent:自定义 User-Agent,不传则使用内置默认值
  • proxy:HTTP/HTTPS 代理地址,不传则直连
result = parse(
    "https://mp.weixin.qq.com/s/xxxxx",
    timeout=30,
    user_agent="MyBot/1.0",
    proxy="http://user:pass@127.0.0.1:7890",
)

返回结构

parseparse_async 均返回 ArticleResult 数据类,包含以下字段:

字段 类型 说明
mp_id_b64 str 公众号 ID(Base64 编码原始值)
mp_id int 公众号 ID(解码后的整数)
mp_name str 公众号名称
mp_alias str 公众号别名
mp_image str 公众号头像链接
mp_description str 公众号简介
mp_account_type AccountType 账号类型:AccountType.SUBSCRIPTION(订阅号)/ AccountType.SERVICE(服务号)/ AccountType.UNKNOWN(未识别)
article_id str 文章 ID
article_msg_id int 文章所在的群发消息 ID
article_idx int 群发图文中的位置(从 1 开始)
article_sn str 文章签名(防伪校验)
article_title str 文章标题
article_cover_image str 文章封面图链接
article_description str 文章摘要
article_markdown str 文章正文的 Markdown 内容
article_publish_time int 发布时间(Unix 时间戳)
images list[str] 文章中提取的所有图片链接
is_valid bool 关键字段是否全部解析成功(属性)

is_validTrue 的条件:mp_idmp_namearticle_idarticle_msg_idarticle_idxarticle_snarticle_titlearticle_markdownarticle_publish_time 均不为空/零。

判断账号类型

AccountType 继承自 str 枚举,既支持枚举比较,也支持与中文字符串直接比较:

from wechat_article_parser import parse, AccountType

result = parse("https://mp.weixin.qq.com/s/xxxxx")

# 推荐:枚举比较(有类型提示与 IDE 补全)
if result.mp_account_type == AccountType.SERVICE:
    print("这是服务号")

# 也支持:字符串字面量比较
if result.mp_account_type == "服务号":
    print("这是服务号")

# 打印直接输出中文值
print(f"账号类型: {result.mp_account_type}")  # 账号类型: 订阅号

异常处理

WeChatVerifyError

当请求频率过高或 IP 被限流时,微信会返回验证码页面而非文章内容。此时会抛出 WeChatVerifyError

from wechat_article_parser import parse, WeChatVerifyError

try:
    result = parse("https://mp.weixin.qq.com/s/xxxxx")
except WeChatVerifyError:
    print("触发了微信人机验证,请稍后重试或更换 IP")

httpx.HTTPStatusError

当 HTTP 请求返回非 2xx 状态码时,会抛出 httpx.HTTPStatusError

import httpx
from wechat_article_parser import parse

try:
    result = parse("https://mp.weixin.qq.com/s/xxxxx")
except httpx.HTTPStatusError as e:
    print(f"请求失败: {e.response.status_code}")

解析不完整

部分文章类型可能无法提取所有字段(如某些文章没有封面图或摘要)。这种情况不会抛出异常,但 result.is_valid 会返回 False。建议在业务逻辑中检查:

result = parse("https://mp.weixin.qq.com/s/xxxxx")
if not result.is_valid:
    print("部分关键字段未能解析")

测试

安装开发依赖:

pip install -e ".[dev]"

运行全部测试

pytest tests/test_parser.py -v -s

只运行同步 / 异步测试

pytest tests/test_parser.py -v -s -k "sync"
pytest tests/test_parser.py -v -s -k "async"

测试单个链接 - 查看所有字段

pytest tests/test_parser.py::test_fetch_all -s --url "https://mp.weixin.qq.com/s/xxxxx"

测试单个链接 - 只看 Markdown 内容

pytest tests/test_parser.py::test_fetch_markdown -s --url "https://mp.weixin.qq.com/s/xxxxx"

通过 HTTP 代理运行测试

所有测试命令都支持 --proxy 参数,传入后所有请求都会经由该代理;不传则直连。常用于 IP 被微信限流时换出口:

# 全量测试走代理
pytest tests/test_parser.py -v -s --proxy "http://127.0.0.1:7890"

# 测试单个链接走代理
pytest tests/test_parser.py::test_fetch_all -s \
  --url "https://mp.weixin.qq.com/s/xxxxx" \
  --proxy "http://user:pass@127.0.0.1:7890"

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

wechat_article_parser-0.0.4.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

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

wechat_article_parser-0.0.4-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file wechat_article_parser-0.0.4.tar.gz.

File metadata

  • Download URL: wechat_article_parser-0.0.4.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for wechat_article_parser-0.0.4.tar.gz
Algorithm Hash digest
SHA256 31229fd9c9a5b425ac439ed8e0b2565eedcebdbbf785e5d11bbb966ff9f78349
MD5 829aaffecf836f6460845f00c6e1d116
BLAKE2b-256 c24f95e149b1f979910e5dd5c238c0da28e324f3bbfe3568640b7c582fc6f54f

See more details on using hashes here.

File details

Details for the file wechat_article_parser-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for wechat_article_parser-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 35fc924e3d25b041dd1f4e6dd138a9733f974ddd1140a8d89491b635a38a6250
MD5 56ac1027586ef343a2aaf73d77f41aaa
BLAKE2b-256 3ad78e05c7f5e19d43f0852ef03f0ce79662cea4724f229434527dacd92a98c4

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