练习使用Python模块,调取微信好友接口,获取好友相关信息进行统计并可视化展示。
Python:Python是一种计算机程序设计语言。是一种动态的、面向对象的脚本语言,最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越来越多被用于独立的、大型项目的开发。
itchat:itchat是一个开源的微信个人号接口,使用python调用微信从未如此简单。
使用不到三十行的代码,你就可以完成一个能够处理所有信息的微信机器人。
pyecharts:pyecharts 是一个用于生成 Echarts 图表的类库。Echarts 是百度开源的一个数据可视化 JS 库。用 Echarts 生成的图可视化效果非常棒,为了与 Python 进行对接,方便在 Python 中直接使用数据生成图。
pandas:python的一个数据分析包,纳入了大量库和一些标准的数据模型,数据结构包含有一维数组Series、二维的表格型数据结构DataFrame、三维的数组Panel 。
Python 3.6
PyCharm
anaconda3 安装见我的博客
pip install itchat -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
pip install echarts-countries-pypkg -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
pip install echarts-china-provinces-pypkg -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
pip install pyecharts -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
pip install pyecharts_snapshot -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
# -*- coding: utf-8 -*-?
import pandas as pd
import itchat
from pyecharts import Pie, Map, Style, Page, Bar
# from pyecharts import configure
# configure(global_theme='dark')
# 获取联系人信息
def get_attr(friends, key):#get_attr()函数,根据键值获取好友列表的方法:
return list(map(lambda user: user.get(key), friends))
# 获取联系人信息
def get_friends():#编写get_friends()函数,使用itchat模块的get_friends()方法获取微信好友信息:
# 连接微信登陆的入口,hotReload记住登陆状态
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
# 获取联系人性别
def sex_stats(users):#编写sex_stats()函数和prov_stats()函数,从读取到的好友信息中提取并统计好友性别及所在省份:
# 将数据格式化
df = pd.DataFrame(users)
# 将性别信息提取出来
sex_arr = df.groupby(['sex'], as_index=True)['sex'].count()
# 将提取出来的性别信息转化为我们需要的格式
data = dict(zip(list(sex_arr.index), list(sex_arr)))
# 因为微信接口性别给的是:0、1、2,所以需要我们自己将其转化
data['None'] = data.pop(0)
data['Male'] = data.pop(1)
data['Female'] = data.pop(2)
return data.keys(), data.values()
# 获取联系人省份
def prov_stats(users):
# 将数据格式化
prv = pd.DataFrame(users)
# 将省份信息提取出来
prv_cnt = prv.groupby('province', as_index=True)['province'].count().sort_values()
# 将提取出来的省份信息转化为我们需要的格式
attr = list(map(lambda x: x if x != '' else 'None', list(prv_cnt.index)))
return attr, list(prv_cnt)
# 将数据格式化
df = pd.DataFrame(users)
# 将城市信息提取出来
data = df.query('province == "Beijing"')
res = data.groupby('city', as_index=True)['city'].count().sort_values()
# 将提取出来的城市信息转化为我们需要的格式
attr = list(map(lambda x: '%sDistrict' % x if x != '' else 'None', list(res.index)))
return attr, list(res)
# 生成图表
def create_charts():#得到我们需要的信息后,现在开始创建图表,编写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('Sex Ratio of WeChat Friends-Pie Chart') # title_pos='center'
# chart.use_theme('chalk')
# 添加数据
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)
# 生成图片
page.add(chart)
#############################
data = prov_stats(users)
attr, value = data
chart = Map('Friends Distribution-Map of China', **style.init_style)
# chart.use_theme('chalk')
chart.add('', attr, value, is_label_show=True, is_visualmap=True, visual_text_color='#001')
page.add(chart)
chart = Bar('Friends Distribution-Bar Chart', **style_middle.init_style)
# chart.use_theme('chalk')
chart.add('', attr, value, is_stack=True, label_pos='top', is_datazoom_show=True, is_label_show=True)
# 生成图片
page.add(chart)
page.render()
if __name__ == '__main__':
create_charts()
users = get_friends()