Scrape and Analyze Your WeChat Friends with Python and R
This article demonstrates how to use the Python itchat library to extract personal WeChat friend data, then analyzes gender ratios, city distribution, and signature word clouds with R and Python visualisation tools, offering a practical guide for personal social network analytics.
WeChat, with nearly 900 million users, is a super‑app that many rely on for chatting, sharing, reading, and payments. The author used Python's itchat package, which provides a personal‑account API for WeChat, to crawl their own friend list and explore insights such as gender ratios and geographic distribution.
After installing itchat via the terminal, the script logs in by displaying a QR code, scans it, and then retrieves friend information into a friends data structure.
Gender Ratio of My WeChat Friends
The gender field is stored under the key Sex (1 for male, 2 for female, others for unknown). By iterating over the data, the author calculated the following percentages:
Male friends: 37.65%
Female friends: 59.23%
Unknown gender: 3.12%
City Distribution of My WeChat Friends
Each friend entry also contains nickname, province, city, and personal signature. By extracting these fields into a data frame and preprocessing with R, the author grouped and aggregated the data by province and city, then plotted the top 20 locations using ggplot2. The results show a concentration in Guangdong (especially Guangzhou, Shenzhen, Zhuhai), followed by Auckland, Sichuan, Macau, etc., with many friends having no location set (NA).
Word Cloud of Friends' Signatures
The author also collected each friend's personal signature, cleaned it by removing emojis, HTML tags, and punctuation with regular expressions, and concatenated the text. Using the jieba segmentation library, the text was tokenised, then visualised as a word cloud shaped like the author's avatar with matplotlib, wordcloud, numpy, and PIL.
The resulting word cloud reveals frequent themes such as listening, kindness, soul, perseverance, and dreams among the friends' signatures.
Conclusion
The experiment shows how easy it is to open a new window into one's social network using Python and R for data extraction and visual analysis, and hints at further possibilities with itchat like auto‑reply, friend addition, and group management.
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.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
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.
