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()
到这就结束了哦