国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

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

2020-02-16 10:46:21
字體:
來源:轉載
供稿:網友

前言

最近在一個微信公眾號上看到一個調用微信 API 可以對微信好友進行簡單數據分析的一個包 itchat 感覺挺好用的,就簡單嘗試了一下。

庫文檔說明鏈接在這: itchat

安裝

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

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

sudo pip3 install itchat --upgrade

通過該命令判斷是否安裝成功:

python3 -c "import itchat"

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

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

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

#coding:utf-8import 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, Piechart = 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-8import itchatimport reitchat.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 jiebawordlist_jieba = jieba.cut(text, cut_all=True)wl_space_split = " ".join(wordlist_jieba)# wordcloud詞云import matplotlib.pyplot as pltfrom wordcloud import WordCloud, ImageColorGeneratorimport osimport numpy as npimport PIL.Image as Imaged= 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')            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临汾市| 修武县| 琼结县| 蕉岭县| 孟连| 翁源县| 司法| 东源县| 宜昌市| 吉木萨尔县| 江门市| 龙里县| 哈密市| 漯河市| 义马市| 吉首市| 五大连池市| 泸水县| 谷城县| 库尔勒市| 莎车县| 武隆县| 公主岭市| 苗栗市| 饶河县| 渝中区| 嘉定区| 进贤县| 威宁| 翼城县| 焉耆| 河源市| 特克斯县| 凤山县| 梁河县| 娄底市| 合江县| 南汇区| 扬州市| 华蓥市| 龙里县|