国产探花免费观看_亚洲丰满少妇自慰呻吟_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')            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 犍为县| 大名县| 玉环县| 漳浦县| 和林格尔县| 邛崃市| 贺兰县| 吉安市| 稻城县| 达州市| 青川县| 茂名市| 安陆市| 大足县| 金塔县| 衡水市| 克山县| 中卫市| 永善县| 金沙县| 临湘市| 遂平县| 南康市| 遂平县| 万全县| 鄯善县| 双辽市| 福鼎市| 达拉特旗| 广河县| 扎兰屯市| 庆云县| 天柱县| 宁城县| 承德县| 铜山县| 天门市| 新安县| 苍山县| 塘沽区| 鄂州市|