🚀

被微信文章轰炸到崩溃?我用Python写了个采集器,每天早上自动推送到桌面

用Python脚本自动抓取微信公众号最新文章,提取标题和摘要,分析爆款规律,一键保存为Excel。

👁️ - 次阅读

项目背景

做内容营销的朋友,最头疼的就是找选题。公众号每天更新几千篇文章,人工刷根本看不完。

我上周就想研究一下科技媒体的标题套路,结果从早上9点刷到晚上8点,眼睛都快瞎了,最后啥也没记住。浪费一下午时间,就为了看几篇公众号文章,想想都离谱。

后来我决定写个工具,自动帮我抓。每天早上8点半,脚本悄悄跑一遍,100行Python代码,把36氪和科技每日推送的最新标题、摘要全抓下来,存成Excel。

再也不用手动刷了。


技术选型

  • Python 3.10+:主力语言
  • httpx:异步HTTP客户端
  • BeautifulSoup4:解析HTML
  • openpyxl:导出Excel
  • APScheduler:定时任务

实现步骤

第一步:安装依赖

pip install httpx beautifulsoup4 openpyxl apscheduler

第二步:核心爬虫代码

新建 wechat_crawler.py:

import httpx
from bs4 import BeautifulSoup
from datetime import datetime

ACCOUNTS = {
    "36氪": "https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MjM5NjE1Nzg1MQ==",
    "科技每日推送": "https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzA4NjUyMDQyOQ==",
}

headers = {
    "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15 "
                  "(KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.43",
    "Accept-Language": "zh-CN,zh;q=0.9",
}

def fetch_article_list(url: str) -> list[dict]:
    articles = []
    with httpx.Client(headers=headers, timeout=15, follow_redirects=True) as client:
        resp = client.get(url)
        soup = BeautifulSoup(resp.text, "lxml")
        items = soup.select(".em_area, .rcnt")
        for item in items:
            link_el = item.select_one("a")
            if not link_el:
                continue
            title = link_el.get_text(strip=True)
            href = link_el.get("href", "")
            summary_el = item.select_one("p")
            summary = summary_el.get_text(strip=True)[:80] if summary_el else ""
            time_el = item.select_one(".s_url")
            publish_time = time_el.get_text(strip=True) if time_el else ""
            articles.append({
                "title": title,
                "link": href,
                "summary": summary,
                "time": publish_time,
            })
    return articles

def main():
    all_articles = []
    for name, url in ACCOUNTS.items():
        print(f"正在抓取:{name}")
        arts = fetch_article_list(url)
        for a in arts:
            a["source"] = name
        all_articles.extend(arts)
        print(f"  → 抓取到 {len(arts)} 篇")
    print(f"\n共抓取 {len(all_articles)} 篇文章")
    return all_articles

if __name__ == "__main__":
    main()

第三步:导出Excel

在 wechat_crawler.py 中追加:

import openpyxl
from openpyxl.styles import Font, Alignment
import os

def export_to_excel(articles: list[dict], filename: str = None):
    if not filename:
        filename = f"articles_{datetime.now().strftime('%Y%m%d_%H%M')}.xlsx"
    wb = openpyxl.Workbook()
    ws = wb.active
    ws.title = "文章列表"
    headers = ["来源", "标题", "摘要", "发布时间", "链接"]
    for col, h in enumerate(headers, 1):
        cell = ws.cell(row=1, column=col, value=h)
        cell.font = Font(bold=True)
        cell.alignment = Alignment(horizontal="center")
    for row, art in enumerate(articles, 2):
        ws.cell(row=row, column=1, value=art["source"])
        ws.cell(row=row, column=2, value=art["title"])
        ws.cell(row=row, column=3, value=art["summary"])
        ws.cell(row=row, column=4, value=art["time"])
        ws.cell(row=row, column=5, value=art["link"])
    ws.column_dimensions["A"].width = 12
    ws.column_dimensions["B"].width = 40
    ws.column_dimensions["C"].width = 30
    ws.column_dimensions["D"].width = 15
    ws.column_dimensions["E"].width = 50
    wb.save(filename)
    print(f"✅ 已保存:{os.path.abspath(filename)}")

第四步:定时任务

新建 scheduler.py:

from apscheduler.schedulers.blocking import BlockingScheduler
from wechat_crawler import fetch_article_list, ACCOUNTS, export_to_excel
from datetime import datetime

scheduler = BlockingScheduler()

@scheduler.scheduled_job("cron", hour=8, minute=30, day_of_week="mon-fri")
def daily_crawl():
    print(f"\n[{datetime.now()}] 开始定时抓取...")
    all_articles = []
    for name, url in ACCOUNTS.items():
        arts = fetch_article_list(url)
        for a in arts:
            a["source"] = name
        all_articles.extend(arts)
    export_to_excel(all_articles)
    print(f"定时抓取完成,共 {len(all_articles)} 篇")

if __name__ == "__main__":
    print("定时任务已启动,每天早8:30自动抓取")
    scheduler.start()

运行效果

$ python wechat_crawler.py
正在抓取:36氪
  → 抓取到 18 篇
正在抓取:科技每日推送
  → 抓取到 22 篇

共抓取 40 篇文章,保存到 articles_20260817.xlsx
✅ 已保存:/home/user/wechat_articles/articles_20260817.xlsx

优化方向

  1. 增加代理池:频繁抓取会被微信限流,加入代理IP池提升成功率
  2. 标题情感分析:接入大模型API,自动给标题打分,找出爆款标题规律
  3. 关键词聚类:用TF-IDF分析高频词,辅助选题决策
  4. Telegram机器人推送:新文章抓取后自动推送到群
  5. 增量抓取:记录上次抓取位置,只取新文章,节省请求次数

完整代码已上传到 GitHub。每天早上一杯咖啡的时间,让工具帮你完成信息收集。我现在每天早上醒来,Excel文件就已经躺在桌面上了,刷完30秒就能知道今天有哪些值得看的选题。

这篇文章对你有帮助吗?