Python利用itchat對微信中好友數(shù)據(jù)實現(xiàn)簡單分析的方法
前言
最近在一個微信公眾號上看到一個調(diào)用微信 API 可以對微信好友進行簡單數(shù)據(jù)分析的一個包 itchat 感覺挺好用的,就簡單嘗試了一下。
庫文檔說明鏈接在這: itchat
安裝
在終端中輸入以下命令,完成微信的API包itchat的安裝。
我們這里使用python3的環(huán)境(python2也是可行的):
sudo pip3 install itchat --upgrade
通過該命令判斷是否安裝成功:
python3 -c "import itchat"
如果沒有報錯信息說明你已經(jīng)將實驗環(huán)境安裝完成。
微信好友數(shù)據(jù)進行分析示例
首先統(tǒng)計一下微信好友的男女比例:
#coding:utf-8 import itchat # 先登錄 itchat.login() # 獲取好友列表 friends = itchat.get_friends(update=True)[0:] # 初始化計數(shù)器,有男有女,當(dāng)然,有些人是不填的 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 # 總數(shù)算上,好計算比例啊~ 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")
效果如圖:(不知道為什么還有那么多 其他。。。)
然后抓取所有好友的個性簽名,看看其中的高頻詞匯:
# 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ā)送到手機 my_wordcloud.to_file(os.path.join(d, "wechat_cloud.png")) itchat.send_image("wechat_cloud.png", 'filehelper')
效果如圖:
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- python-itchat 獲取微信群用戶信息的實例
- Python微信庫:itchat的用法詳解
- python實現(xiàn)微信接口(itchat)詳細介紹
- 利用python微信庫itchat實現(xiàn)微信自動回復(fù)功能
- python itchat實現(xiàn)微信自動回復(fù)的示例代碼
- python itchat實現(xiàn)微信好友頭像拼接圖的示例代碼
- python-itchat 統(tǒng)計微信群、好友數(shù)量,及原始消息數(shù)據(jù)的實例
- itchat-python搭建微信機器人(附示例)
- Python3 itchat實現(xiàn)微信定時發(fā)送群消息的實例代碼
- Python使用微信itchat接口實現(xiàn)查看自己微信的信息功能詳解
相關(guān)文章
Django中使用Json返回數(shù)據(jù)的實現(xiàn)方法
這篇文章主要介紹了Django中使用Json返回數(shù)據(jù)的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06利用matplotlib實現(xiàn)兩張子圖分別畫函數(shù)圖
這篇文章主要介紹了利用matplotlib實現(xiàn)兩張子圖分別畫函數(shù)圖問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08python數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之實現(xiàn)線性表的順序
這篇文章主要為大家詳細介紹了python數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之實現(xiàn)線性表的順序,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-09-09python運算符+條件結(jié)構(gòu)+循環(huán)結(jié)構(gòu)
這篇文章主要介紹了python運算符、條件結(jié)構(gòu)、循環(huán)結(jié)構(gòu);算術(shù)運算符、賦值運算符、邏輯運算符等一些相關(guān)內(nèi)容,需要的小伙伴可以參考一下,希望對你的學(xué)習(xí)有所幫助2022-03-03