熱線電話:13121318867

登錄
首頁精彩閱讀Python利用itchat對微信中好友數據實現簡單分析的方法
Python利用itchat對微信中好友數據實現簡單分析的方法
2018-07-22
收藏

Python利用itchat對微信中好友數據實現簡單分析的方法

Python 熱度一直很高,我感覺這就是得益于擁有大量的包資源,極大的方便了開發人員的需求。下面這篇文章主要給大家介紹了關于Python利用itchat實現對微信中好友數據進行簡單分析的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下。
前言
最近在一個微信公眾號上看到一個調用微信 API 可以對微信好友進行簡單數據分析的一個包 itchat 感覺挺好用的,就簡單嘗試了一下。
庫文檔說明鏈接在這: itchat
安裝
在終端中輸入以下命令,完成微信的API包itchat的安裝。
我們這里使用python3的環境(python2也是可行的):    
sudo pip3 install itchat --upgrade
通過該命令判斷是否安裝成功:    
python3 -c "import itchat"

如果沒有報錯信息說明你已經將實驗環境安裝完成。


微信好友數據進行分析示例

首先統計一下微信好友的男女比例:

#coding:utf-8
importitchat
# 先登錄
itchat.login()
# 獲取好友列表
friends=itchat.get_friends(update=True)[0:]
# 初始化計數器,有男有女,當然,有些人是不填的
male=female=other=0
# 遍歷這個列表,列表里第一位是自己,所以從"自己"之后開始計算# 1表示男性,2女性
foriinfriends[1:]:
 sex=i["Sex"]
 ifsex==1:
 male+=1
 elifsex==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,加上這段
fromechartsimportEchart, 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"]))
delchart.json["xAxis"]
delchart.json["yAxis"]
chart.plot()
chart.save("/Library","phones")

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



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

# coding:utf-8
importitchat
importre
itchat.login()
friends=itchat.get_friends(update=True)[0:]
tList=[]
foriinfriends:
 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分詞
importjieba
wordlist_jieba=jieba.cut(text, cut_all=True)
wl_space_split=" ".join(wordlist_jieba)
# wordcloud詞云
importmatplotlib.pyplot as plt
fromwordcloudimportWordCloud, ImageColorGenerator
importos
importnumpy as np
importPIL.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')

效果如圖:

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值



數據分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數據分析師資訊
更多

OK
客服在線
立即咨詢
日韩人妻系列无码专区视频,先锋高清无码,无码免费视欧非,国精产品一区一区三区无码
客服在線
立即咨詢