Analyzing Your WeChat Friends with Python: Gender, Avatar, Signature & Location
This tutorial shows how to use Python libraries such as itchat, jieba, matplotlib, snownlp, and Tencent Youtu to fetch WeChat friend data and then visualize gender distribution, detect face usage in avatars, generate word clouds and sentiment analysis for signatures, and export location information for mapping.
Introduction
With the widespread use of WeChat, many people rely on it for daily communication and work. This article demonstrates how to perform data analysis on WeChat friends using Python, focusing on gender, avatar, signature, and location dimensions, and presenting results with charts and word clouds.
Required Third‑Party Modules
itchat : Python wrapper for the WeChat web interface, used to retrieve friend information.
jieba : Chinese word segmentation library for processing text.
matplotlib : Plotting library for creating bar and pie charts.
snownlp : Chinese sentiment analysis library.
PIL : Image processing library.
numpy : Numerical computation, used together with wordcloud.
wordcloud : Generates word‑cloud images.
TencentYoutuyun : SDK for Tencent Youtu API, used for face detection and image tag extraction.
All modules can be installed via pip.
Fetching Friend Data
Login to WeChat through itchat and obtain the friend list. The first element is the logged‑in user; the actual friends start from friends[1:].
itchat.auto_login(hotReload=True)
friends = itchat.get_friends(update=True)Gender Analysis
Extract the Sex field, count Male, Female, and Unknown values, and plot a pie chart.
def analyseSex(friends):
sexs = list(map(lambda x: x['Sex'], friends[1:]))
counts = list(map(lambda x: x[1], Counter(sexs).items()))
labels = ['Unknow', 'Male', 'Female']
colors = ['red', 'yellowgreen', 'lightskyblue']
plt.figure(figsize=(8,5), dpi=80)
plt.axes(aspect=1)
plt.pie(counts, labels=labels, colors=colors, labeldistance=1.1,
autopct='%3.1f%%', shadow=False, startangle=90, pctdistance=0.6)
plt.legend(loc='upper right')
plt.title(u"%s的微信好友性别组成" % friends[0]['NickName'])
plt.show()The resulting pie chart shows the gender composition of the friend list.
Avatar Analysis
Download each friend's avatar, use Tencent Youtu to detect whether the image contains a face and to extract image tags. The face‑presence statistics are shown with a pie chart, while the extracted tags are visualized as a word cloud.
def analyseHeadImage(friends):
basePath = os.path.abspath('.')
baseFolder = basePath + '\\HeadImages\\'
if not os.path.exists(baseFolder):
os.makedirs(baseFolder)
faceApi = FaceAPI()
use_face = 0
not_use_face = 0
image_tags = ''
for index in range(1, len(friends)):
friend = friends[index]
imgFile = baseFolder + '\\Image%s.jpg' % str(index)
imgData = itchat.get_head_img(userName=friend['UserName'])
if not os.path.exists(imgFile):
with open(imgFile, 'wb') as file:
file.write(imgData)
time.sleep(1)
if faceApi.detectFace(imgFile):
use_face += 1
else:
not_use_face += 1
result = faceApi.extractTags(imgFile)
image_tags += ','.join([x['tag_name'] for x in result])
labels = [u'使用人脸头像', u'不使用人脸头像']
counts = [use_face, not_use_face]
colors = ['red', 'yellowgreen', 'lightskyblue']
plt.figure(figsize=(8,5), dpi=80)
plt.axes(aspect=1)
plt.pie(counts, labels=labels, colors=colors, labeldistance=1.1,
autopct='%3.1f%%', shadow=False, startangle=90, pctdistance=0.6)
plt.legend(loc='upper right')
plt.title(u"%s的微信好友使用人脸头像情况" % friends[0]['NickName'])
plt.show()
# Word cloud for tags
back_coloring = np.array(Image.open('face.jpg'))
wordcloud = WordCloud(font_path='simfang.ttf', background_color='white',
max_words=1200, mask=back_coloring, max_font_size=75,
random_state=45, width=800, height=480, margin=15)
wordcloud.generate(image_tags)
plt.imshow(wordcloud)
plt.axis('off')
plt.show()Signature Analysis
Combine jieba keyword extraction and SnowNLP sentiment analysis on the Signature field. Generate a word cloud of frequent keywords and a bar chart of sentiment distribution.
def analyseSignature(friends):
signatures = ''
emotions = []
for friend in friends:
signature = friend.get('Signature')
if signature:
signature = re.sub(r'1f(\d.+)', '', signature)
if len(signature) > 0:
nlp = SnowNLP(signature)
emotions.append(nlp.sentiments)
signatures += ' '.join(jieba.analyse.extract_tags(signature, 5))
with open('signatures.txt', 'wt', encoding='utf-8') as file:
file.write(signatures)
# Word cloud
back_coloring = np.array(Image.open('flower.jpg'))
wordcloud = WordCloud(font_path='simfang.ttf', background_color='white',
max_words=1200, mask=back_coloring, max_font_size=75,
random_state=45, width=960, height=720, margin=15)
wordcloud.generate(signatures)
plt.imshow(wordcloud)
plt.axis('off')
plt.show()
# Sentiment bar chart
count_good = len([x for x in emotions if x > 0.66])
count_normal = len([x for x in emotions if 0.33 <= x <= 0.66])
count_bad = len([x for x in emotions if x < 0.33])
labels = [u'负面消极', u'中性', u'正面积极']
values = (count_bad, count_normal, count_good)
plt.rcParams['font.sans-serif'] = ['simHei']
plt.rcParams['axes.unicode_minus'] = False
plt.xlabel(u'情感判断')
plt.ylabel(u'频数')
plt.xticks(range(3), labels)
plt.bar(range(3), values, color='rgb')
plt.title(u"%s的微信好友签名信息情感分析" % friends[0]['NickName'])
plt.show()Location Analysis
Extract Province and City fields, write them to a CSV file, and then use external tools (e.g., BDP) to create a geographic distribution map.
def analyseLocation(friends):
headers = ['NickName', 'Province', 'City']
with open('location.csv', 'w', encoding='utf-8', newline='') as csvFile:
writer = csv.DictWriter(csvFile, headers)
writer.writeheader()
for friend in friends[1:]:
row = {
'NickName': friend['NickName'],
'Province': friend['Province'],
'City': friend['City']
}
writer.writerow(row)Conclusion
The article demonstrates a complete workflow for analyzing WeChat friends from four perspectives—gender, avatar, signature, and location—using Python for data collection, processing, and visualization. It emphasizes that visualization is a means to reveal insights rather than an end goal.
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
