微信是大概大家使用最频繁的社交工具了,每个微信好友都有一个微信名片。
本文的目标就是对好友列表中的性别、省份进行统计并对好友的个性签名生成词云。
本文使用到的关键包为:wordcloud、itchat
下面配合代码进行讲解:
import itchat as it
it.login()
friends=it.get_friends(update=True)[0:]
print(type(friends))
#性别
dict_sex=dict()
dict_sex[0]=0#未知
dict_sex[1]=0#男
dict_sex[2]=0#女
#省份
dict_province=dict()
for user in friends:
print()
print('user.NickName:',user.NickName)
print('user.Sex:', user.Sex)
print('user.City:', user.City)
print('user.Signature:',user.Signature)
print('user.Province:', user.Province)
dict_sex[user.Sex] = dict_sex[user.Sex] + 1
if dict_province.keys().__contains__(user.Province):
dict_province[user.Province]=dict_province[user.Province]+1
else:
dict_province[user.Province]=1
#性别统计结果
print('none:',dict_sex[0])
print('male:',dict_sex[1])
print('female:',dict_sex[2])
#省份统计结果
print(dict_province)
#出去特殊字符
import re
siglist = []
for i in friends:
signature = i["Signature"].strip().replace('span','').replace('class','').replace('emoji','').replace('\n','')
rep = re.compile("1f\d+\w*|[<>/=]")
signature = rep.sub("", signature)
siglist.append(signature)
text = "".join(siglist)
#分词
import jieba
wordlist = jieba.cut(text, cut_all=True)
word_space_split = " ".join(wordlist)
print(word_space_split)
#绘制词云
import matplotlib.pyplot as plt
from wordcloud import WordCloud, ImageColorGenerator
import numpy as np
import PIL.Image as Image
coloring = np.array(Image.open("./img/8615600.jpg"))#自定义词云的图片
my_wordcloud = WordCloud(background_color="white", max_words=2000,
mask=coloring, max_font_size=60, random_state=42,font_path='./font/DroidSansFallbackFull.ttf',scale=2).generate(word_space_split)#wget http://labfile.oss.aliyuncs.com/courses/756/DroidSansFallbackFull.ttf中文字符文件
image_colors = ImageColorGenerator(coloring)
plt.imshow(my_wordcloud.recolor(color_func=image_colors))
plt.imshow(my_wordcloud)
plt.axis("off")
plt.show()
从结果上看,我的好友状态都不错呀~