Parsing WeChat Public Account Articles via API and Python Script

This guide demonstrates how to retrieve a list of WeChat public account articles by constructing the appropriate API URL, examining its query parameters, viewing the JSON response, and using a Python script to parse the data and generate a markdown file of titles and links.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Parsing WeChat Public Account Articles via API and Python Script

Enter the WeChat public account management backend, write an article, and notice the place to insert a hyperlink.

Here we extract the link and analyze its parameters. The begin parameter indicates the start position, and count indicates the number of items. The following link starts from 0 and retrieves 50 articles.

https://mp.weixin.qq.com/cgi-bin/appmsg?action=list_ex&begin=0&count=50&fakeid=&type=9&query=&token=1234455&lang=zh_CN&f=json&ajax=1

Open this URL directly in a browser; the page returns JSON data.

Use a JSON formatter tool.

Now we know how to parse the titles and links; write a script to automate the process. The JSON data is manually obtained via an HTML page, then processed with a Python script, and finally written to a markdown file.

import json
import os 

if __name__ == "__main__":
  for j in os.listdir("./"):
      if j.endswith(".json"):
          f = open(j, 'r')
          data = f.read()
          f.close()

          jdata = json.loads(data)
          print("%s data : %s" % (j, jdata.keys()))

          for doc in jdata['app_msg_list']:
              strFormat = "[%s](%s)" % (doc['title'], doc['link'])
              with open('data.txt','a+') as f:
                  f.writelines(strFormat.encode('utf-8') + "

")
              f.close()

Result:

That's all; the code does not include detailed condition checks (multithreading, etc.) but is sufficient for the task.

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.

PythonJSONAPIWeChatWeb Scrapingmarkdown
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

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.