How to Use Python to Extract All Articles from a WeChat Public Account via the Official API

This article explains how to write a Python crawler that uses the WeChat public platform's open API to obtain an access token, retrieve the list of articles from a specified public account, and extract each article's title, summary, and link for further analysis.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
How to Use Python to Extract All Articles from a WeChat Public Account via the Official API

When we want to extract all articles from a specific WeChat public account, we can use the platform's open API and write a Python crawler.

1. Import required libraries

import requests
import json

2. Send request to get article list

def get_article_list(public_account, count=10):
    # Construct request URL
    url = f"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET"
    # Send GET request to obtain access token
    response = requests.get(url)
    access_token = response.json()["access_token"]
    # Construct article list URL
    article_url = f"https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token={access_token}"
    # Request body
    data = {
        "type": "news",
        "offset": 0,
        "count": count
    }
    # Send POST request to get articles
    response = requests.post(article_url, data=json.dumps(data))
    # Parse JSON response
    articles = response.json()["item"]
    return articles

3. Process article data

def process_articles(articles):
    # Process each article
    for article in articles:
        title = article["title"]
        summary = article["digest"]
        url = article["url"]
        print("标题:", title)
        print("摘要:", summary)
        print("链接:", url)
        print()

4. Call functions and output results

public_account = "公众号名称"
count = 10
# Get article list
articles = get_article_list(public_account, count)
# Process article data
process_articles(articles)

The script fetches an access token, retrieves the article list, and prints each article’s title, digest, and link, providing a simple way to collect and analyze WeChat public account content.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonBackend DevelopmentData ExtractionWeb ScrapingWeChat API
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.