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.
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 json2. 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 articles3. 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
