今天要介绍如何利用Python实现统计微信好友数目,省排布
首先我们要用到的几个库以及安装这些库的指令
from wxpy import *import pandas as pd#初始化机器人,选择缓存模式(扫码)登录bot = Bot(cache_path=True)#获取我的所有微信好友信息
接下来的代码就是将用户的好友的微信名,性别,城市,省份以及个性签名作为内容保存到一个Excel表格中
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(‘wx1.xlsx‘,encoding=‘\U0001f31a‘) print(1)
再接着的代码是构建一个有关用户的所在城市的词云
toex(lis)import pandas as pdfrom pyecharts import WordCloud df=pd.read_excel(‘wx1.xlsx‘)city_list = df[‘城市‘].fillna(‘‘).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‘wx1place.html‘)print(1)
最后的是将用户的好友所在城市的标记在中国地图上并以地图的形式展示出来
import pandas as pdfrom pyecharts import Map df=pd.read_excel(‘wx1.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)
最后的结果
这个是获取的表格信息
下图是生成的词云图
最后是生成的地图