How to Extract and Visualize WeChat Friend Data with Python itchat
This tutorial demonstrates how to log into WeChat via the itchat library, retrieve friends' region, gender, and signature information, store it as JSON, and generate visualizations such as gender distribution charts, province maps, and word clouds using Python.
Preface
This blog post is the third article in the itchat library series and focuses on obtaining WeChat friends' region, gender, and signature information and visualizing the results.
Main Content
Getting Friend Information with itchat
Use the following two lines of code:
<ol><li><code>itchat.auto_login(hotReload=True)</code></li><li><code>friends = itchat.get_friends(update=True)</code></li></ol>The first line logs into the WeChat web client; hotReload=True allows re‑login without scanning the QR code if the program restarts shortly after exiting. get_friends() returns a list of dictionaries, one per friend. Because the list is serializable, you can save it to a file with the json module:
<ol><li><code># Convert the JSON object to a string and save locally</code></li><li><code>with open("data.json", "w", encoding="utf-8") as f:</code></li><li><code> f.write(json.dumps(friends, indent=2, ensure_ascii=False))</code></li></ol>Sample output is shown below:
Extracting Data and Visualizing
Gender Visualization
Count gender values with the following loop:
<ol><li><code>for friend in friends:</code></li><li><code> if friend["Sex"] == 0:</code></li><li><code> unknow += 1</code></li><li><code> elif friend["Sex"] == 1:</code></li><li><code> male += 1</code></li><li><code> else:</code></li><li><code> female += 1</code></li></ol>The resulting chart shows that male friends dominate while females account for about a quarter; "unknown" represents friends who have not set a gender.
Region Visualization
Collect province data with:
<ol><li><code>dic_province = dict()</code></li><li><code>for friend in friends:</code></li><li><code> key = friend["Province"]</code></li><li><code> if key not in dic_province:</code></li><li><code> dic_province[key] = 1</code></li><li><code> else:</code></li><li><code> dic_province[key] += 1</code></li><li><code>key_list = []</code></li><li><code>value_list = []</code></li><li><code>for key, value in dic_province.items():</code></li><li><code> if key == "":</code></li><li><code> key = "其他地区"</code></li><li><code> key_list.append(key)</code></li><li><code> value_list.append(value)</code></li></ol>The visualization reveals the distribution of friends across provinces, inadvertently exposing the author’s own location.
Signature (Signature) Visualization
Aggregate all signatures and filter out emojis before generating a word cloud:
<ol><li><code>all_sign = ""</code></li><li><code>for friend in friends:</code></li><li><code> sign = friend["Signature"]</code></li><li><code> if len(sign) > 0:</code></li><li><code> emoji = re.findall("<span class=.*></span>", sign, re.S)</code></li><li><code> if len(emoji) > 0:</code></li><li><code> sign = sign.replace(emoji[0], "")</code></li><li><code> all_sign = all_sign + sign + "
"</code></li><li><code>if os.path.exists("wc/sign.txt"):</code></li><li><code> os.remove("wc/sign.txt")</code></li><li><code>with open("wc/sign.txt", "a+", encoding="utf-8") as f:</code></li><li><code> f.write(all_sign)</code></li></ol>The generated word cloud shows a group of enthusiastic friends discussing life.
Other Visualizations
Similar techniques can be applied to other friend attributes; details are omitted for brevity.
Running the Demo
Execute main.py in the project root to generate gender and region visualizations (see images above). If you haven’t logged in recently, the program will prompt you to scan the QR code.
To visualize signatures, run wc.py inside the wc folder. The word‑cloud background can be customized by replacing wc/image/bc.png with a solid‑color image.
All Source Code
Reply "微信好友" to the public account backend to obtain the full source.
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.
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!
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.
