python微信好友签名

#功能:请输出微信好友的签名,格式(好友昵称,好友签名,情感极性)

#功能:还能实现词云分析,自定义画图

 

import itchat

import re

from snownlp  import SnowNLP#自然语言处理

from wordcloud import WordCloud

from matplotlib import pyplot as plt

import jieba

import numpy as np

import PIL.Image as Image

#爬取自己好友相关信息,返回一个json文件

 

itchat.auto_login(hotReload=True)

friends=itchat.get_friends(update=True)[0:]

 

def getSignature():  #获取好友签名,并对其进行情感分析

    with open("sign.txt",'w',encoding="utf-8") as file:

        for f in friends:

            nickname=f["NickName"]

            rec1=re.compile("[^\u4e00-\u9fa5^]")

            nickname=rec1.sub("",nickname)

            signature=f["Signature"].strip().replace("emoji","").replace("span","").replace("class","")

            rec=re.compile("[^\u4e00-\u9fa5^]")

            signature=rec.sub("",signature)

#情感分析

            if signature !="":

                s=SnowNLP(signature)

                if s.sentiments>=0.55:

                    file.write("积极地签名:")

                    file.write(nickname+"\n")

                    file.write(signature+"\n")

                elif s.sentiments<0.45:

                    file.write("消极的签名:")

                    file.write(nickname+"\n")

                    file.write(signature+"\n")

                else:

                    file.write("一般地签名:")

                    file.write(nickname+"\n")

                    file.write(signature+"\n")

 

def create_wordcloud(filename):                 #绘制词云

    text=open(filename,encoding="utf-8").read()

    wordlist=jieba.cut(text,cut_all=True)

    wl=" ".join(wordlist)

    coloring=np.array(Image.open("he1.png"))

    wc=WordCloud(background_color='white',max_words=300,font_path="C:\\Windows\\Fonts\\simfang.ttf",

                 height=500,width=500,max_font_size=60,random_state=30,mask=coloring)

    myword=wc.generate(wl)

    plt.imshow(myword)

    plt.axis("off")

    plt.show()

    wc.to_file("sign.png")

    itchat.send_image("sign.png","filehelper")

 

getSignature()

create_wordcloud("sign.txt")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值