Python利用itchat對(duì)微信中好友數(shù)據(jù)實(shí)現(xiàn)簡(jiǎn)單分析的方法
前言
最近在一個(gè)微信公眾號(hào)上看到一個(gè)調(diào)用微信 API 可以對(duì)微信好友進(jìn)行簡(jiǎn)單數(shù)據(jù)分析的一個(gè)包 itchat 感覺挺好用的,就簡(jiǎn)單嘗試了一下。
庫(kù)文檔說明鏈接在這: itchat
安裝
在終端中輸入以下命令,完成微信的API包itchat的安裝。
我們這里使用python3的環(huán)境(python2也是可行的):
sudo pip3 install itchat --upgrade
通過該命令判斷是否安裝成功:
python3 -c "import itchat"
如果沒有報(bào)錯(cuò)信息說明你已經(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è)列表,列表里第一位是自己,所以從"自己"之后開始計(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à)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- python-itchat 獲取微信群用戶信息的實(shí)例
- Python微信庫(kù):itchat的用法詳解
- python實(shí)現(xiàn)微信接口(itchat)詳細(xì)介紹
- 利用python微信庫(kù)itchat實(shí)現(xiàn)微信自動(dòng)回復(fù)功能
- python itchat實(shí)現(xiàn)微信自動(dòng)回復(fù)的示例代碼
- python itchat實(shí)現(xiàn)微信好友頭像拼接圖的示例代碼
- python-itchat 統(tǒng)計(jì)微信群、好友數(shù)量,及原始消息數(shù)據(jù)的實(shí)例
- itchat-python搭建微信機(jī)器人(附示例)
- Python3 itchat實(shí)現(xiàn)微信定時(shí)發(fā)送群消息的實(shí)例代碼
- Python使用微信itchat接口實(shí)現(xiàn)查看自己微信的信息功能詳解
相關(guān)文章
Python基于Flask框架配置依賴包信息的項(xiàng)目遷移部署
這篇文章主要介紹了Python基于Flask框架配置依賴包信息的項(xiàng)目遷移部署小技巧,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-03-03Django中使用Json返回?cái)?shù)據(jù)的實(shí)現(xiàn)方法
這篇文章主要介紹了Django中使用Json返回?cái)?shù)據(jù)的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06利用matplotlib實(shí)現(xiàn)兩張子圖分別畫函數(shù)圖
這篇文章主要介紹了利用matplotlib實(shí)現(xiàn)兩張子圖分別畫函數(shù)圖問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08python數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之實(shí)現(xiàn)線性表的順序
這篇文章主要為大家詳細(xì)介紹了python數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之實(shí)現(xiàn)線性表的順序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09python運(yùn)算符+條件結(jié)構(gòu)+循環(huán)結(jié)構(gòu)
這篇文章主要介紹了python運(yùn)算符、條件結(jié)構(gòu)、循環(huán)結(jié)構(gòu);算術(shù)運(yùn)算符、賦值運(yùn)算符、邏輯運(yùn)算符等一些相關(guān)內(nèi)容,需要的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)有所幫助2022-03-03scrapy實(shí)踐之翻頁(yè)爬取的實(shí)現(xiàn)
這篇文章主要介紹了scrapy實(shí)踐之翻頁(yè)爬取的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01100?個(gè)?Python?小例子(練習(xí)題四)
這篇文章主要給大家分享100?個(gè)?Python?小例子,前文分享了一二三,本文的四十最后一篇了,這篇就把100道python小練習(xí)全分享完了,感興趣的小伙伴也可以去練習(xí)前幾期內(nèi)容,洗碗給這幾篇文章給你的學(xué)習(xí)帶來幫助2022-01-01Python實(shí)現(xiàn)EM算法實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于Python實(shí)現(xiàn)EM算法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10