Analyzing WeChat Friend Data with Python: Gender, Avatar, Signature, and Location Insights
This tutorial demonstrates how to use Python libraries such as itchat, jieba, matplotlib, SnowNLP, and Tencent Youtu SDK to collect WeChat friend information and perform data analysis and visualization on gender distribution, avatar characteristics, signature sentiment, and geographic location, providing practical code examples and visual results.
The article introduces a Python‑based workflow for extracting and analyzing WeChat friend data, focusing on four dimensions: gender, avatar, signature, and location.
Required third‑party modules are listed, including itchat for fetching friend info, jieba for Chinese word segmentation, matplotlib for charting, snownlp for sentiment analysis, PIL for image handling, numpy and wordcloud for word‑cloud generation, and TencentYoutuyun SDK for face detection and image tagging.
Login and friend retrieval are performed with two lines of code:
itchat.auto_login(hotReload = True)</code><code>friends = itchat.get_friends(update = True)Gender analysis extracts the Sex field from each friend, counts Male, Female, and Unknown values, and visualizes the distribution with a pie chart using matplotlib. The core function is:
def analyseSex(firends):</code><code> sexs = list(map(lambda x:x['Sex'],friends[1:]))</code><code> counts = list(map(lambda x:x[1],Counter(sexs).items()))</code><code> labels = ['Unknow','Male','Female']</code><code> colors = ['red','yellowgreen','lightskyblue']</code><code> plt.figure(figsize=(8,5), dpi=80)</code><code> plt.axes(aspect=1)</code><code> plt.pie(counts, labels=labels, colors=colors, labeldistance=1.1, autopct='%3.1f%%', shadow=False, startangle=90, pctdistance=0.6)</code><code> plt.legend(loc='upper right')</code><code> plt.title(u'%s的微信好友性别组成' % friends[0]['NickName'])</code><code> plt.show()Avatar analysis downloads each friend's head image, uses the Tencent Youtu FaceAPI to detect whether the avatar contains a human face, and extracts image tags. Results are shown with a pie chart for face‑presence and a word cloud for tag frequencies. Key code snippet:
def analyseHeadImage(frineds):</code><code> basePath = os.path.abspath('.')</code><code> baseFolder = basePath + '\HeadImages\'</code><code> if(os.path.exists(baseFolder) == False): os.makedirs(baseFolder)</code><code> faceApi = FaceAPI()</code><code> use_face = 0; not_use_face = 0; image_tags = ''</code><code> for index in range(1,len(friends)):</code><code> friend = friends[index]</code><code> imgFile = baseFolder + '\Image%s.jpg' % str(index)</code><code> imgData = itchat.get_head_img(userName = friend['UserName'])</code><code> if(os.path.exists(imgFile) == False):</code><code> with open(imgFile,'wb') as file: file.write(imgData)</code><code> time.sleep(1)</code><code> result = faceApi.detectFace(imgFile)</code><code> if result == True: use_face += 1 else: not_use_face += 1</code><code> result = faceApi.extractTags(imgFile)</code><code> image_tags += ','.join(list(map(lambda x:x['tag_name'],result)))</code><code> # plot pie chart and wordcloud (omitted for brevity)Signature analysis extracts the Signature field, cleans it, performs Jieba keyword extraction for a word cloud, and uses SnowNLP to compute sentiment scores. The sentiment distribution is visualized with a bar chart. Core function:
def analyseSignature(friends):</code><code> signatures = ''</code><code> emotions = []</code><code> pattern = re.compile("1f\d.+")</code><code> for friend in friends:</code><code> signature = friend['Signature']</code><code> if(signature != None):</code><code> signature = signature.strip().replace('span','').replace('class','').replace('emoji','')</code><code> signature = re.sub(r'1f(\d.+)','',signature)</code><code> if(len(signature)>0):</code><code> nlp = SnowNLP(signature)</code><code> emotions.append(nlp.sentiments)</code><code> signatures += ' '.join(jieba.analyse.extract_tags(signature,5))</code><code> # generate wordcloud and sentiment bar chart (omitted)Location analysis extracts Province and City fields, writes them to a CSV file, and suggests using Baidu BDP (or similar tools) to create a geographic distribution map. Example code:
def analyseLocation(friends):</code><code> headers = ['NickName','Province','City']</code><code> with open('location.csv','w',encoding='utf-8',newline='') as csvFile:</code><code> writer = csv.DictWriter(csvFile, headers)</code><code> writer.writeheader()</code><code> for friend in friends[1:]:</code><code> row = {'NickName':friend['NickName'],'Province':friend['Province'],'City':friend['City']}</code><code> writer.writerow(row)The article concludes that data visualization is a means rather than an end, emphasizing the insights derived from the charts rather than the charts themselves, and hopes the example inspires readers to apply similar techniques to their own data.
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 Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
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.
