import itchat
import re#正则匹配
# 先登录,扫二维码登录微信
itchat.login()
#获取好友列表,返回的是json信息
friends = itchat.get_friends(update=True)[0:]
#打印好友列表信息
#print(friends)
tList = []
for i in friends:
# 获取个性签名,替换掉span,class,emoji
signature = i["Signature"].replace(" ", "").replace("span", "").replace("class", "").replace("emoji", "")
# 正则匹配过滤掉emoji表情,例如emoji1f3c3等
rep = re.compile("1f\d.+")#创建正则匹配对象
signature = rep.sub("", signature)#用signature替换空格
tList.append(signature)
# 拼接字符串
text = "".join(tList)
Getting uuid of QR code.
Downloading QR code.
Please scan the QR code to log in.
Please press confirm on your phone.
Loading the contact, this may take a little while.
Login successfully as 孤久则惯
import jieba
wordlist_jieba = jieba.cut(text, cut_all=True)
wl_space_split = " ".join(wordlist_jieba)
# wordcloud词云
import matplotlib.pyplot as plt
from wordcloud import WordCloud, ImageColorGenerator
import os
import numpy as np
import PIL.Image as Image
#d = os.path.dirname(__file__)
#找一张微信logo图来生成配色方案,微信logo图wechat.jpg路径在F:\\盘下
alice_coloring = np.array(Image.open(os.path.join('2.jpg')))
#这里要选择字体存放路径,win的字体在C:/windows/Fonts中
# """#my_wordcloud = WordCloud().generate(wl_space_split) 默认构造函数
# my_wordcloud = WordCloud(
# background_color='white', # 设置背景颜色 //这边的颜色可以修改成很多= =就是词云所在图的后面的颜色
# mask = abel_mask, # 设置背景图片
# max_words = 200, # 设置最大显示的字数
# stopwords = STOPWORDS, # 设置停用词
# font_path = C:/Users/Windows/fonts/simkai.ttf', # 设置字体格式,如不设置显示不了中文
# max_font_size = 50, # 设置字体最大值
# random_state = 30, # 设置有多少种随机生成状态,即有多少种配色方案
# scale=.5
# ).generate(wl_space_split)"""
my_wordcloud = WordCloud(background_color="white", max_words=2000, mask=alice_coloring,max_font_size=40, random_state=42,font_path='C:/Windows/Fonts/simhei.ttf') \
.generate(wl_space_split)
image_colors = ImageColorGenerator(alice_coloring)
plt.imshow(my_wordcloud.recolor(color_func=image_colors))
plt.imshow(my_wordcloud)
plt.axis("off")
plt.show()
# 保存图片到F:\\盘下 并发送到手机里的文件传输助手(filehelper)里
my_wordcloud.to_file(os.path.join('wechat_cloud.png'))
itchat.send_image("wechat_cloud.png", 'filehelper')
//配色图的意思是以所用图的颜色和基础形状为基准生成词云的图片,字体大小为出现的频率多少
for each in friends:
print(each['NickName']+" "+str(each['Sex']))
孤久则惯 1
after all. 1
...
参考:Eileen0214大大, https://blog.csdn.net/Eileen0214/article/details/80051326