1. 使用到的库
① wxpy:初始化微信机器人
② openpyxl:保存微信好友数据为Excel表格
③ pyecharts:生成可视化的地图
④ wordcloud、matplotlib、jieba:生成词云图
【特别提醒】:pyecharts 库用的是0.5.x版本,而在 pip 中安装的为1.x.x版本
2. 基本功能
① 分析微信好友数据
② 生成词云图
③ 生成地图展示
3. 代码实现
先从微信中下载信息并保存为html格式
-
- # -*- coding: utf-8 -*-
- """
- Created on Wed Jun 5 13:02:31 2019
- @author: ASUS
- """
- 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)
显示效果: