1、所需安裝庫html
* itchat:微信網頁版接口封裝Python版本,在本文中用以獲取微信好友信息。
* jieba:結巴分詞的 Python 版本,在本文中用以對文本信息進行分詞處理。
* matplotlib: Python 中圖表繪製模塊,在本文中用以繪製柱形圖和餅圖
* snownlp:一個 Python 中的中文分詞模塊,在本文中用以對文本信息進行情感判斷。
* PIL: Python 中的圖像處理模塊,在本文中用以對圖片進行處理。
* numpy: Python中 的數值計算模塊,在本文中配合 wordcloud 模塊使用。
* wordcloud: Python 中的詞雲模塊,在本文中用以繪製詞雲圖片。
* pyecharts:生成可視化的地圖
以上模塊都可經過 pip 安裝,關於各個模塊使用的詳細說明,請自行查閱各自文檔。python
2、依賴庫介紹git
① 分析微信好友數據github
② 生成詞雲圖緩存
③ 生成地圖展現微信
4、運行代碼app
from wxpy import* bot=Bot(cache_path=True) friend_all=bot.friends()
運行代碼後會跳出二維碼,打開手機掃描贊成後,進入微信頁面獲取好友信息echarts
接下來是統計好友個數函數
而後呢製做列表將好友列表保存到excel中,以便後面的使用工具
lis=[] for a_friend in friend_all: NickName=a_friend.raw.get('NickName',None) #Sex=a_friend.raw.get('Sex',None) Sex={1:"男",2:"女",0:"其它"}.get(a_friend.raw.get('Sex',None),None) City=a_friend.raw.get('City',None) Province=a_friend.raw.get('Province',None) Signature=a_friend.raw.get('Signature',None) HeadImgUrl=a_friend.raw.get('HeadImgUrl',None) HeadImgFlag=a_friend.raw.get('HeadImgFlag',None) list_0=[NickName,Sex,City,Province,Signature,HeadImgUrl,HeadImgFlag] lis.append(list_0) def lis2e07(filename,lis): import openpyxl wb=openpyxl.Workbook() sheet=wb.active sheet.title='list2excel07' file_name=filename+'.xlsx' for i in range(0,len(lis)): for j in range(0,len(lis[i])): sheet.cell(row=i+1,column=j+1,value=str(lis[i][j])) wb.save(file_name) print("寫入數據成功")
接下來在運行中敲下代碼
lis2e07('yubgl',lis)
就會跳出:寫入數據成功!
接下來就在保存的文件位置中找到yubgl.xlsx
對好友數據進行分析
最後製做詞雲
完整代碼是這樣的
from wxpy import * #初始化機器人,選擇緩存模式(掃碼)登陸 bot = Bot(cache_path=True) #獲取個人全部微信好友信息 friend_all = bot.friends() print(friend_all[0].raw) len(friend_all) lis=[] for a_friend in friend_all: NickName = a_friend.raw.get('NickName',None) #Sex = a_friend.raw.get('Sex',None) Sex ={1:"男",2:"女",0:"其它"}.get(a_friend.raw.get('Sex',None),None) City = a_friend.raw.get('City',None) Province = a_friend.raw.get('Province',None) Signature = a_friend.raw.get('Signature',None) HeadImgUrl = a_friend.raw.get('HeadImgUrl',None) HeadImgFlag = a_friend.raw.get('HeadImgFlag',None) list_0=[NickName,Sex,City,Province,Signature,HeadImgUrl,HeadImgFlag] lis.append(list_0) def lis2e07(filename,lis): import openpyxl wb = openpyxl.Workbook() sheet = wb.active sheet.title = 'list2excel07' file_name = filename +'.xlsx' for i in range(0, len(lis)): for j in range(0, len(lis[i])): sheet.cell(row=i+1, column=j+1, value=str(lis[i][j])) wb.save(file_name) print("寫入數據成功!") #lis2e07('wx',lis) Friends = bot.friends() data = Friends.stats_text(total=True, sex=True,top_provinces=2, top_cities=3) print(data) from pandas import read_excel df = read_excel('yubgl.xlsx',sheetname='list2excel07') df.tail(5) df['city'].count() df['city'].describe() from wordcloud import WordCloud import matplotlib.pyplot as plt import pandas as pd from pandas import DataFrame word_list= df['city'].fillna('0').tolist() new_text = ' '.join(word_list) wordcloud = WordCloud(font_path='simhei.ttf', background_color="black").generate(new_text) plt.imshow(wordcloud) plt.axis("off") plt.show()
到這就結束了哦