python利用微信的方法_Python利用itchat对微信中好友数据实现简单分析的方法

python 的 Python利用itchat对微信中好友数据实现简单分析的方法

前言

最近在一个微信公众号上看到一个调用微信 API 可以对微信好友进行简单数据分析的一个包 itchat 感觉挺好用的,就简单尝试了一下。

库文档说明链接在这:

安装

在终端中输入以下命令,完成微信的API包itchat的安装。

我们这里使用python3的环境(python2也是可行的):

sudo pip3 install itchat --upgrade

通过该命令判断是否安装成功:

python3 -c "import itchat"

如果没有报错信息说明你已经将实验环境安装完成。

1514950F060-4Y948.jpg

微信好友数据进行分析示例

首先统计一下微信好友的男女比例:

#coding:utf-8

import itchat

# 先登录

itchat.login()

# 获取好友列表

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

# 初始化计数器,有男有女,当然,有些人是不填的

male = female = other = 0

# 遍历这个列表,列表里第一位是自己,所以从"自己"之后开始计算# 1表示男性,2女性

for i in friends[1:]:

sex = i["Sex"]

if sex == 1:

male += 1

elif sex == 2:

female += 1

else:

other += 1

# 总数算上,好计算比例啊~

total = len(friends[1:])

# 好了,打印结果

print (u"男性好友:%.2f%%" % (float(male) / total * 100))

print (u"女性好友:%.2f%%" % (float(female) / total * 100))

print (u"其他:%.2f%%" % (float(other) / total * 100))

# 使用echarts,加上这段

from echarts import Echart, Legend, Pie

chart = Echart(u'%s的微信好友性别比例' % (friends[0]['NickName']), 'from WeChat')

chart.use(Pie('WeChat',[{'value': male, 'name': u'男性 %.2f%%' % (float(male) / total * 100)},{'value': female, 'name': u'女性 %.2f%%' % (float(female) / total * 100)},{'value': other, 'name': u'其他 %.2f%%' % (float(other) / total * 100)}],radius=["50%", "70%"]))

chart.use(Legend(["male", "female", "other"]))

del chart.json["xAxis"]

del chart.json["yAxis"]

chart.plot()

chart.save("/Library","phones")

效果如图:(不知道为什么还有那么多 其他。。。)

1514950F0L0-492I1.jpg

然后抓取所有好友的个性签名,看看其中的高频词汇:

# coding:utf-8

import itchat

import re

itchat.login()

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

tList = []

for i in friends:

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

rep = re.compile("1f\d.+")

signature = rep.sub("", signature)

tList.append(signature)

# 拼接字符串

text = "".join(tList)

# jieba分词

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__)

alice_coloring = np.array(Image.open(os.path.join(d, "wechat.jpg")))

my_wordcloud = WordCloud(background_color="white", max_words=2000,mask=alice_coloring,max_font_size=40, random_state=42,font_path='/Users/sebastian/Library/Fonts/Arial Unicode.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()

# 保存图片 并发送到手机

my_wordcloud.to_file(os.path.join(d, "wechat_cloud.png"))

itchat.send_image("wechat_cloud.png", 'filehelper')

效果如图:

1514950F0b0-501916.jpg

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值