搜索和读取微信公众号文章的完整工具,支持 simple(快速)和 playwright(稳定)双模式 + auto 自动切换。
bash
python3 scripts/search.py "关键词" [数量]
示例:
bash
python3 scripts/search.py "绿电直连政策" 10
bash
python3 scripts/read.py "微信文章URL" [--mode MODE] [--screenshot PATH]
模式选择:
--mode=simple - 快速模式(requests + BeautifulSoup)--mode=playwright - 稳定模式(真实浏览器)--mode=auto - 自动切换(默认,推荐)python
import sys
sys.path.append('scripts')
from wechat_articles import search_articles, read_article
# 搜索文章
articles = search_articles("绿电直连政策", top_num=5)
# 读取文章(建议加错误处理)
try:
content = read_article(articles[0]['url'], mode='auto')
print(f"标题: {content['title']}")
print(f"公众号: {content['author']}")
print(f"发布时间: {content['publish_time']}") # 若有
print(f"读取模式: {content['mode']}")
for p in content['paragraphs'][:10]:
print(p)
except Exception as e:
print(f"读取失败: {e}")
# auto 模式失败时会抛出异常,建议捕获后降级处理或提示用户
read_article() 返回一个字典,包含以下字段:
字段类型说明titlestr文章标题authorstr公众号名称publish_timestr发布时间(部分文章可能为空)paragraphslist[str]正文段落列表modestr实际使用的读取模式( simple 或 playwright)
search_articles() 返回列表,每项包含:
字段类型说明titlestr文章标题urlstr文章链接(有时效性,建议尽快读取)authorstr公众号名称digeststr文章摘要
模式速度资源稳定性适用场景simple快 (0.5-1s)轻量一般简单页面,频繁调用playwright慢 (3-5s)较重很高复杂页面,稳定优先auto自适应自适应最佳默认推荐
bash
pip install beautifulsoup4 requests miku-ai
这些包通常已随 agent-reach 安装。
bash
pip install playwright
playwright install chromium --with-deps
--with-deps 会自动安装 Linux 系统依赖(如 libnss3、libgbm 等),首次运行需几分钟。
欢迎反馈 & PR!GitHub: https://github.com/johan-oilman/wechat-articles