最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python利用itchat對(duì)微信中好友數(shù)據(jù)實(shí)現(xiàn)簡(jiǎn)單分析的方法

 更新時(shí)間:2017年11月21日 09:30:28   作者:壹言  
Python 熱度一直很高,我感覺(jué)這就是得益于擁有大量的包資源,極大的方便了開(kāi)發(fā)人員的需求。下面這篇文章主要給大家介紹了關(guān)于Python利用itchat實(shí)現(xiàn)對(duì)微信中好友數(shù)據(jù)進(jìn)行簡(jiǎn)單分析的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。

前言

最近在一個(gè)微信公眾號(hào)上看到一個(gè)調(diào)用微信 API 可以對(duì)微信好友進(jìn)行簡(jiǎn)單數(shù)據(jù)分析的一個(gè)包 itchat 感覺(jué)挺好用的,就簡(jiǎn)單嘗試了一下。

庫(kù)文檔說(shuō)明鏈接在這: itchat

安裝

在終端中輸入以下命令,完成微信的API包itchat的安裝。

我們這里使用python3的環(huán)境(python2也是可行的):

sudo pip3 install itchat --upgrade

通過(guò)該命令判斷是否安裝成功:

python3 -c "import itchat"

如果沒(méi)有報(bào)錯(cuò)信息說(shuō)明你已經(jīng)將實(shí)驗(yàn)環(huán)境安裝完成。

微信好友數(shù)據(jù)進(jìn)行分析示例

首先統(tǒng)計(jì)一下微信好友的男女比例:

#coding:utf-8
import itchat
# 先登錄
itchat.login()
# 獲取好友列表
friends = itchat.get_friends(update=True)[0:]
# 初始化計(jì)數(shù)器,有男有女,當(dāng)然,有些人是不填的
male = female = other = 0
# 遍歷這個(gè)列表,列表里第一位是自己,所以從"自己"之后開(kāi)始計(jì)算# 1表示男性,2女性
for i in friends[1:]:
 sex = i["Sex"]
 if sex == 1:
 male += 1
 elif sex == 2:
 female += 1
 else:
 other += 1
 # 總數(shù)算上,好計(jì)算比例啊~
 total = len(friends[1:])
 # 好了,打印結(jié)果

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

效果如圖:(不知道為什么還有那么多 其他。。。)

然后抓取所有好友的個(gè)性簽名,看看其中的高頻詞匯:

# 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()
# 保存圖片 并發(fā)送到手機(jī)
my_wordcloud.to_file(os.path.join(d, "wechat_cloud.png"))
itchat.send_image("wechat_cloud.png", 'filehelper')

效果如圖:

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論

白河县| 南陵县| 新建县| 怀安县| 崇仁县| 安岳县| 永寿县| 巴林左旗| 扶风县| 赣州市| 延长县| 龙门县| 宁陵县| 常州市| 桃园市| 花垣县| 西乌| 罗山县| 门头沟区| 新郑市| 图片| 宁国市| 息烽县| 永丰县| 蓝山县| 思茅市| 紫云| 儋州市| 连云港市| 万载县| 清徐县| 吴江市| 濮阳市| 阿合奇县| 芜湖县| 资阳市| 潜江市| 绍兴市| 东台市| 沾益县| 光泽县|