Operations 6 min read

Automate WeChat Moments Extraction with PyWinAuto and PyAutoGui

This article demonstrates how to use Python's PyWinAuto and PyAutoGui libraries to automatically control the WeChat desktop client, open the Moments window, inspect its UI hierarchy, and extract post content, providing a lightweight alternative to manual scraping or complex hook-based methods.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Automate WeChat Moments Extraction with PyWinAuto and PyAutoGui

Background

Collecting data from WeChat Moments has long been a target for crawlers, but traditional methods require either manual copy‑pasting or complex hook implementations. With the recent support for Moments in the Windows version of WeChat, a simpler automation approach becomes possible.

Required Libraries

psutil – used to obtain the process information of the WeChat desktop client.

pywinauto – provides UI automation capabilities for Windows applications.

Getting the Process ID

import psutil
import pywinauto
from pywinauto.application import Application

PID = 0
for proc in psutil.process_iter():
    try:
        pinfo = proc.as_dict(attrs=['pid', 'name'])
    except psutil.NoSuchProcess:
        pass
    else:
        if 'WeChat.exe' == pinfo['name']:
            PID = pinfo['pid']

The obtained PID is later passed to PyWinAuto to connect to the WeChat process.

Connecting to WeChat with PyWinAuto

app = Application(backend='uia').connect(process=PID)

Opening the Moments Window

win = app['微信']
pyq_btn = win.child_window(title="朋友圈", control_type="Button")
cords = pyq_btn.rectangle()
pywinauto.mouse.click(button='left', coords=(cords.left+10, cords.top+10))

The above code opens the Moments window in the WeChat desktop client.

Inspecting the UI Tree

Use the .dump_tree() method to print the component hierarchy of the Moments window, which helps locate the controls that contain the data you need.

Example screenshot of the Moments UI after automation:

Extracting Content

By locating the Edit component within the UI tree, you can retrieve the text of a specific Moment post.

Conclusion

Using PyWinAuto together with PyAutoGui offers a straightforward and effective way to automate the WeChat desktop client, open the Moments window, and extract the desired data without the need for complex hooking or manual copying.

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.

UI automationWeChatPyWinAutopyautogui
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

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.