1. 使用到的庫 html
① wxpy:初始化微信機器人微信
② openpyxl:保存微信好友數據爲Excel表格app
③ pyecharts:生成可視化的地圖echarts
④ wordcloud、matplotlib、jieba:生成詞雲圖spa
【特別提醒】:pyecharts 庫用的是0.5.x版本,而在 pip 中安裝的爲1.x.x版本excel
2. 基本功能 htm
① 分析微信好友數據blog
② 生成詞雲圖ip
③ 生成地圖展現utf-8
3. 代碼實現
先從微信中下載信息並保存爲html格式
-
- from wxpy import *
- import pandas as pd
- bot = Bot(cache_path=True)
- friend_all = bot.friends()
- lis=[]
- for a_friend in friend_all:
- NickName = a_friend.raw.get('NickName',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)
- list_0=[NickName,Sex,City,Province,Signature]
- lis.append(list_0)
- def toex(lis):
- text=pd.DataFrame(lis,columns=['微信名','性別','城市','省份','個性簽名'])
- text.to_excel('wx.xlsx',encoding='\U0001f31a')
- print(1)
- toex(lis)
顯示結果以下:

利用得到的數據製造詞雲
-
# -*- coding: utf-8 -*-
-
"""
-
Created on Wed Jun 5 14:07:47 2019
-
-
@author: ASUS
-
"""
-
-
import pandas as pd
-
from pyecharts import WordCloud
-
df=pd.read_excel('wx.xlsx')
-
city_list = df['城市'].fillna('city').tolist()
-
count_city = pd.value_counts(city_list)
-
name = count_city.index.tolist()
-
value = count_city.tolist()
-
wordcloud=WordCloud(width=1300, height=620)
-
wordcloud.add("", name, value, word_size_range=[20, 100])
-
wordcloud.show_config()
-
wordcloud.render(r'wxcity.html')
-
print(1)

用pyecharts製做地圖:
-
# -*- coding: utf-8 -*-
-
"""
-
Created on Wed Jun 5 14:08:21 2019
-
-
@author: ASUS
-
"""
-
-
import pandas as pd
-
from pyecharts import Map
-
df=pd.read_excel('wx.xlsx')
-
pr_list = df['省份'].fillna('pr').tolist()
-
count_pr = pd.value_counts(pr_list)
-
attr =count_pr.index.tolist()
-
value = count_pr.tolist()
-
maap=Map("各省微信好友分佈", width=1200, height=600)
-
maap.add("", attr, value, maptype='china', is_visualmap=True,visual_text_color='#000', is_label_show = True)
-
maap.show_config()
-
maap.render(r'wxpr.html')
-
print(1)
顯示效果:
