个人兴趣爱好,通过python对微信朋友圈进行了分析,主要对微信好友进行提取,对好友地区分布,签名等进行可视化
需要安装包如下:
pip3 install itchat
pip3 install pandas
pip3 install echarts-countries-pypkg
pip3 install echarts-china-provinces-pypkg
pip3 install echarts-china-cities-pypkg
pip3 install pyecharts
pip3 install jieba
pip3 install wordcloud
pip3 install numpy
获取微信圈好友信息
def get_attr(friends, key):
return list(map(lambda user: user.get(key), friends))
def get_friends():
itchat.auto_login(hotReload=True)
friends = itchat.get_friends()
users = dict(province=get_attr(friends, "Province"),
city=get_attr(friends, "City"),
nickname=get_attr(friends, "NickName"),
sex=get_attr(friends, "Sex"),
signature=get_attr(friends, "Signature"),
remarkname=get_attr(friends, "RemarkName"),
pyquanpin=get_attr(friends, "PYQuanPin"),
displayname=get_attr(friends, "DisplayName"),
isowner=get_attr(friends, "IsOwner"))
return users
itchat.auto_login(hotReload=True) 登录微信圈好友,hotReload参数表示短时间内不需要扫码可登陆(在项目下生成itchat.pkl文件),执行上述代码,弹出登录二维码,只需要拿出手机—>扫码登录即可。处理后数据为一个dict
数据分析
- 好友性别
先来看下朋友圈好友性别比例
def sex_stats(users):
df = pd.DataFrame(users)
sex_arr = df.groupby(['sex'], as_index=True)['sex'].count()
data = dict(zip(list(sex_arr.index), list(sex_arr)))
data['不告诉你'] = data.pop(0)
data['帅哥'] = data.pop(1)
data['美女'] = data.pop(2)
return data.keys(), data.values()
def create_charts():
users = get_friends()
page = Page()
style = Style(width=1100, height=600)
style_middle = Style(width=900, height=500)
data = sex_stats(users)
attr, value = data
chart = Pie('微信性别') # title_pos='center'
chart.add('', attr, value, center=[50, 50],
radius=[30, 70], is_label_show=True, legend_orient='horizontal', legend_pos='center',
legend_top='bottom', is_area_show=True)
pandas 为数据分析工具,类似数据库中的表。df.groupby(['sex'], as_index=True)['sex'].count() 按性别统计好友数,Pie为环形图类。帅哥占比65.3,美女31.51,本人屌丝程序员一枚,所有帅哥比较多
- 省份分布
再来看看各省份好友分布情况
def gd_stats(users):
df = pd.DataFrame(users)
data = df.query('province == "广东"')
res = data.groupby('city', as_index=True)['city'].count().sort_values()
attr = list(map(lambda x: '%s市' % x if x != '' else '未知', list(res.index)))
return attr, list(res)
def create_charts():
page.add(chart)
data = prov_stats(users)
attr, value = data
chart = Map('中国地图', **style.init_style)
chart.add('', attr, value, is_label_show=True, is_visualmap=True, visual_text_color='#000')
page.add(chart)
chart = Bar('柱状图', **style_middle.init_style)
chart.add('', attr, value, is_stack=True, is_convert=True, label_pos='inside', is_legend_show=True,
is_label_show=True)
page.add(chart)
data = gd_stats(users)
attr, value = data
chart = Map('广东', **style.init_style)
chart.add('', attr, value, maptype='广东', is_label_show=True, is_visualmap=True, visual_text_color='#000')
page.add(chart)
chart = Bar('柱状图', **style_middle.init_style)
chart.add('', attr, value, is_stack=True, is_convert=True, label_pos='inside', is_label_show=True)
page.add(chart)
page.render()
- 好友山东省分布
def gd_stats(users):
df = pd.DataFrame(users)
data = df.query('province == "山东"')
res = data.groupby('city', as_index=True)['city'].count().sort_values()
attr = list(map(lambda x: '%s市' % x if x != '' else '未知', list(res.index)))
return attr, list(res)