Skip to main content

微信公众号 API 等相关工具

Project description

微信公众号 API 工具

微信公众平台(微信公众号)API 相关工具,提供公众号搜索、文章列表获取、文章内容下载等功能。

项目地址: https://github.com/morning-start/wxmp


项目简介

wxmp 是一个 Python 库,用于与微信公众平台后台 API 进行交互。通过本项目可以:

  • 通过微信登录后的 cookies 获取访问 token
  • 搜索公众号(根据关键词查找公众号信息)
  • 获取指定公众号的文章列表
  • 验证文章链接有效性
  • 下载文章内容并转换为 Markdown 格式
  • 支持时间范围缓存和增量更新
  • 支持并发下载

核心特性

  • API 封装: 完整封装微信公众平台后台 API
  • 缓存优化: 支持 FakeID 缓存和时间范围缓存,避免重复获取
  • 并发下载: 支持多线程并发下载文章内容
  • 格式转换: 自动将 HTML 文章转换为 Markdown 格式
  • 灵活扩展: 用户可直接使用已有爬虫或基于 API 构建自定义爬虫
  • 增量更新: 支持时间范围缓存,只获取新增文章

技术栈

技术 版本 用途
Python >= 3.10 开发语言
Pydantic >= 2.12.5 数据模型验证
Requests >= 2.32.5 HTTP 请求
Pandas >= 2.3.3 数据处理
Loguru >= 0.7.3 日志记录
tqdm >= 4.67.3 进度条显示
fake-useragent >= 2.2.0 随机 User-Agent

安装

pip install wxmp

或从源码安装:

git clone https://github.com/morning-start/wxmp.git
cd wxmp
pip install -e .

快速开始

1. 初始化 API

from wxmp import WxMPAPI

cookies = {
    "wxuin": "your_wxuin",
    "pass_ticket": "your_pass_ticket",
}

api = WxMPAPI(cookies)

2. 搜索公众号

response = api.fetch_fakeid("Python")
for account in response.arr:
    print(f"名称: {account.nickname}")
    print(f"FakeID: {account.fakeid}")

3. 使用时间范围爬虫(推荐)

from wxmp.spider import TimeRangeSpider
from wxmp.tools.time_manager import TimeRange
from datetime import datetime
from pathlib import Path

spider = TimeRangeSpider.from_cookies_file("cookies.json")
bizs = spider.load_or_search_bizs(["Python编程"])

time_range = TimeRange(
    begin=datetime(2024, 1, 1),
    end=datetime(2024, 12, 31)
)

df = spider.search_articles_content(bizs, time_range)
spider.save_all_article_content(
    df=df,
    save_dir=Path("temp/article_content/"),
    time_range=time_range
)

项目结构

wxmp/
├── src/wxmp/                    # 源码目录
│   ├── api/                     # API 层 - 基础 API 封装
│   │   ├── __init__.py
│   │   ├── common.py           # 公共模型和异常
│   │   ├── index.py            # WxMPAPI 主类
│   │   ├── token.py            # Token 获取
│   │   ├── search_biz.py       # 公众号搜索 API
│   │   └── list_ex.py          # 文章列表 API
│   ├── spider/                  # Spider 层 - 业务逻辑层
│   │   ├── __init__.py
│   │   └── time_range_spider.py # 时间范围爬虫
│   └── tools/                   # Tools 层 - 工具层
│       ├── __init__.py
│       ├── article.py          # 文章内容处理
│       ├── file.py             # 文件操作工具
│       └── time_manager.py     # 时间范围缓存管理
├── wiki/                        # 项目文档
├── pyproject.toml              # 项目配置
└── README.md                   # 项目说明

文档

详细文档请查看 Wiki

License

MIT License

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

wxmp-2.5.0.tar.gz (17.0 kB view details)

Uploaded Source

Built Distribution

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

wxmp-2.5.0-py3-none-any.whl (23.2 kB view details)

Uploaded Python 3

File details

Details for the file wxmp-2.5.0.tar.gz.

File metadata

  • Download URL: wxmp-2.5.0.tar.gz
  • Upload date:
  • Size: 17.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for wxmp-2.5.0.tar.gz
Algorithm Hash digest
SHA256 1326e2be1850a9c484e83386f88851df34a24204f8f687b148b6d98a064ea4f6
MD5 1bd091346f0d599649b04febff0812d8
BLAKE2b-256 ccccb60339e75766f357e5b236c31ebf49282fe1e0adcd3b32189ca13b7bc65d

See more details on using hashes here.

Provenance

The following attestation bundles were made for wxmp-2.5.0.tar.gz:

Publisher: python-publish.yml on morning-start/wxmp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file wxmp-2.5.0-py3-none-any.whl.

File metadata

  • Download URL: wxmp-2.5.0-py3-none-any.whl
  • Upload date:
  • Size: 23.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for wxmp-2.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4c74613be9abaee64b304d89b6dbd8cbb5725c25f18a8d56dac6a717399d3d2f
MD5 2b9da6659701d3b348d473079e3fa98d
BLAKE2b-256 552c16c0e92ed017e111d952572fdc750b6d8454508566b3f66ad0f818c1324f

See more details on using hashes here.

Provenance

The following attestation bundles were made for wxmp-2.5.0-py3-none-any.whl:

Publisher: python-publish.yml on morning-start/wxmp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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