zoukankan      html  css  js  c++  java
  • 【python】itchat登录微信获取好友签名并生成词云

    在知乎上看到一篇关于如何使用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’

    引用库

     1 #!/user/bin/python
     2 ##coding=utf-8
     3 import itchat
     4 from itchat.content import *
     5 import numpy as np
     6 from matplotlib import pyplot as plt
     7 import re
     8 from wordcloud import WordCloud
     9 from wordcloud import ImageColorGenerator
    10 import os
    11 import io
    12 import jieba
    13 from PIL import Image

    登录微信获取好友男女比例

     1 itchat.login()
     2 text=dict()
     3 friends = itchat.get_friends(update=True)[0:]
     4 male="male"
     5 female="female"
     6 other="other"
     7 for i in friends[1:]:
     8     sex = i['Sex']
     9     if sex == 1:
    10         text[male] = text.get(male, 0) + 1
    11     elif sex == 2:
    12         text[female] = text.get(female, 0) + 1
    13     else:
    14         text[other] = text.get(other, 0) + 1
    15 total = len(friends[1:])
    16 print(total)
    17 print("男性好友: %.2f%%" %(float(text[male]) / total * 100)+"
    " + 
    18     "女性好友:  %.2f%%" %(float(text[female]) / total * 100) + "
    " +
    19     "不明性别好友: %.2f%%" %(float(text[other]) / total * 100))

    使用PLT柱状图绘制男女比例

    1 def draw(datas):
    2     for key in datas.keys():
    3         plt.bar(key,datas[key])
    4 
    5     plt.legend()
    6     plt.xlabel('sex')
    7     plt.ylabel('rate')
    8     plt.title("Gender of Alfred's friends")
    9     plt.show()

    爬取好友签名并保存到文件

     1 def parse_signature():
     2     itchat.login()
     3     siglist = []
     4     friends = itchat.get_friends(update=True)[1:]
     5     for i in  friends:
     6         signature = i["Signature"].strip().replace("span","").replace("class","").replace("emoji","")
     7         rep = re.compile("lfd+w|[<>/=]")
     8         signature = rep.sub("",signature)
     9         siglist.append(signature)
    10     text = "".join(siglist)
    11     with io.open('text.txt', 'a', encoding='utf-8') as f:
    12         wordlist = jieba.cut(text, cut_all=True)
    13         word_space_split = " ".join(wordlist)
    14         f.write(word_space_split)
    15         f.close()

    解析文件并绘制词云

    准备一张图片(8.jpg)放到当前目录下,字体资源准备好(DroidSansFallbackFull.ttf),可以使用everything搜索一下自己电脑中的字体资源拿过来一个用,放到当前目录下,如果没有会提示资源的问题。

    1 def draw_signature():
    2     text = open(u'text.txt', encoding='utf-8').read()
    3     coloring = np.array(Image.open('8.jpg'))
    4     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)
    5     image_colors = ImageColorGenerator(coloring)
    6     plt.imshow(my_wordcloud.recolor(color_func=image_colors))
    7     plt.imshow(my_wordcloud)
    8     plt.axis("off")
    9     plt.show()

    小插曲:微信自动回复

     1 @itchat.msg_register([PICTURE,TEXT])
     2 def simple_reply(msg):
     3     if msg['Type'] == TEXT:
     4         ReplyContent = 'I received message: '+msg['Content']
     5     if msg['Type'] == PICTURE:
     6         ReplyContent = 'I received picture: '+msg['FileName']
     7     itchat.send_msg('nice to meet you',msg['FromUserName'])
     8 
     9 itchat.auto_login()
    10 itchat.run()

    详情去https://zhuanlan.zhihu.com/p/36361397

    参考

    https://itchat.readthedocs.io/zh/latest/

    https://zhuanlan.zhihu.com/p/36361397

  • 相关阅读:
    Ubuntu 虚拟机空间不足增加空间笔记
    am335x sd卡启动开启识别emmc kernel 上的改动
    Delphi实现树型结构具体实例
    学习 TTreeView [15]
    delphi中Treeview的使用介绍
    按下F2编辑dxDBTreeView的节点
    dbtreeview
    我的dbtreeview–treeview直接连接数据表_delphi教程
    SqlDbx连接oracle(可用)
    SqlDbx连接oracle(无需安装Oracle客户端)
  • 原文地址:https://www.cnblogs.com/huangsitao/p/10961867.html
Copyright © 2011-2022 走看看