在知乎上看到一篇关于如何使用 itchat 统计微信好友男女比例并使用 plt 生成柱状图以及获取微信好友签名并生成词云的文章 https://zhuanlan.zhihu.com/p/36361397 , 感觉挺有意思, 就照着学习了下, 发现原文章中没有写明使用的库如何安装和使用到的资源没有说明, 在这里详细的记录下.
本节用到的的库以及安装
itchat
微信相关操作, cmd 中'pip install itchat'
pillow(pil)
Image 用到, cmd 中'pip install pillow'
re
正则表达式
wordcloud
词云, 使用 pip 安装一直提示 Built 时候一直错误, 所以在 https://www.lfd.uci.edu/~gohlke/pythonlibs/ 中找到 wordcloud 对应版本, 然后'pip install wheel 路径 \*.whl'
jieba
分词库, 可以对字符串中的词进行拆分, cmd 中'pip install jieba'
numpy
plt 用到, cmd 中'pip install numpy'
引用库
- #!/user/bin/python
- ##coding=utf-8
- import itchat
- from itchat.content import *
- import numpy as np
- from matplotlib import pyplot as plt
- import re
- from wordcloud import WordCloud
- from wordcloud import ImageColorGenerator
- import os
- import io
- import jieba
- from PIL import Image
登录微信获取好友男女比例
- itchat.login()
- text=dict()
- friends = itchat.get_friends(update=True)[0:]
- male="male"
- female="female"
- other="other"
- for i in friends[1:]:
- sex = i['Sex']
- if sex == 1:
- text[male] = text.get(male, 0) + 1
- elif sex == 2:
- text[female] = text.get(female, 0) + 1
- else:
- text[other] = text.get(other, 0) + 1
- total = len(friends[1:])
- print(total)
- print("男性好友: %.2f%%" %(float(text[male]) / total * 100)+"\n" +
- "女性好友: %.2f%%" %(float(text[female]) / total * 100) + "\n" +
- "不明性别好友: %.2f%%" %(float(text[other]) / total * 100))
使用 PLT 柱状图绘制男女比例
- def draw(datas):
- for key in datas.keys():
- plt.bar(key,datas[key])
- plt.legend()
- plt.xlabel('sex')
- plt.ylabel('rate')
- plt.title("Gender of Alfred's friends")
- plt.show()
爬取好友签名并保存到文件
- def parse_signature():
- itchat.login()
- siglist = []
- friends = itchat.get_friends(update=True)[1:]
- for i in friends:
- signature = i["Signature"].strip().replace("span","").replace("class","").replace("emoji","")
- rep = re.compile("lf\d+\w|[<>/=]")
- signature = rep.sub("",signature)
- siglist.append(signature)
- text = "".join(siglist)
- with io.open('text.txt', 'a', encoding='utf-8') as f:
- wordlist = jieba.cut(text, cut_all=True)
- word_space_split = " ".join(wordlist)
- f.write(word_space_split)
- f.close()
解析文件并绘制词云
准备一张图片 (8.jpg) 放到当前目录下, 字体资源准备好(DroidSansFallbackFull.ttf), 可以使用 everything 搜索一下自己电脑中的字体资源拿过来一个用, 放到当前目录下, 如果没有会提示资源的问题.
- def draw_signature():
- text = open(u'text.txt', encoding='utf-8').read()
- coloring = np.array(Image.open('8.jpg'))
- my_wordcloud = WordCloud(background_color="white", max_words=2000,mask=coloring, max_font_size=300,random_state=42,scale=2,font_path="DroidSansFallbackFull.ttf").generate(text)
- image_colors = ImageColorGenerator(coloring)
- plt.imshow(my_wordcloud.recolor(color_func=image_colors))
- plt.imshow(my_wordcloud)
- plt.axis("off")
- plt.show()
小插曲: 微信自动回复
- @itchat.msg_register([PICTURE,TEXT])
- def simple_reply(msg):
- if msg['Type'] == TEXT:
- ReplyContent = 'I received message:'+msg['Content']
- if msg['Type'] == PICTURE:
- ReplyContent = 'I received picture:'+msg['FileName']
- itchat.send_msg('nice to meet you',msg['FromUserName'])
- itchat.auto_login()
- itchat.run()
完
详情去 https://zhuanlan.zhihu.com/p/36361397
参考
- https://itchat.readthedocs.io/zh/latest/
- https://zhuanlan.zhihu.com/p/36361397
来源: http://www.bubuko.com/infodetail-3079913.html