Automating the Removal of Spam Advertisers in WeChat Groups with Python and pynput
This article describes a Python‑based approach that identifies and eliminates WeChat group members who repeatedly post advertisements by defining heuristic rules, capturing screen coordinates with the pynput library, simulating mouse‑clicks and keyboard shortcuts, and processing the extracted data to flag suspicious accounts.
The author defines a set of heuristic rules (e.g., missing WeChat ID, avatar of a popular female model, special symbols in nickname, no Moments, no background image, and no replies other than join requests) to recognize users who are likely to be spammers in a WeChat group.
Because the official Web WeChat login is disabled, the usual wxpy or itchat libraries cannot be used. Instead, the author turns to desktop automation using pynput , a Python library that can control mouse and keyboard events.
First, pynput is installed with pip install pynput . A simple demo shows how to move the mouse to a specific screen coordinate and click:
from pynput import mouse
mouse.position = (x, y)
mouse.click(mouse.Button.left, 1)To record the exact positions of UI elements, a listener is set up to log every mouse move and click:
from pynput import mouse
def on_move(x, y):
print('Mouse moved to {0}'.format((x, y)))
def on_click(x, y, button, pressed):
print('{0} at {1}'.format('Clicked' if pressed else 'Released', (x, y)))
if not pressed:
return False
while True:
with mouse.Listener(on_move=on_move, on_click=on_click) as listener:
listener.join()With the coordinates of the WeChat icon, the group chat, and each member’s avatar recorded, the script performs the following steps for each member:
Click the WeChat application.
Open the target group chat.
Click a member’s avatar.
Move to the position of the WeChat ID field.
Double‑click to select the ID.
Copy the selected text using Command + C via pynput.keyboard.Controller .
The copied ID is retrieved with the pyperclip library and evaluated (e.g., length or regex) to decide whether it matches the “no WeChat ID” rule:
from pynput.keyboard import Key, Controller as KeyboardController
import pyperclip, time
keyboard = KeyboardController()
with keyboard.pressed(Key.cmd):
keyboard.press('c')
keyboard.release('c')
time.sleep(1)
wechatid = pyperclip.paste()
print(f"WeChat ID {wechatid} {'suspected spam' if len(wechatid) > 20 else 'not spam'}")By iterating over all members, applying the rule set, and optionally scrolling the list, the script flags and can later remove the identified spam accounts. The author reports that the method successfully identified six suspicious accounts, of which two were confirmed and removed.
In the conclusion, the author notes the limitations: the approach can only verify visible fields (name, ID, avatar) and cannot access hidden information such as Moments; the automation is slow because it mimics human interaction and relies on fixed screen coordinates, which break if the window moves. Future improvements could involve more robust element locating techniques.
Sohu Tech Products
A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.
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.