Python微信好友分析Demo

Python微信好友分析Demo

【实验内容】

练习使用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 安装见我的博客

【实验步骤】

  1. 在终端:使用pip命令安装实验用到的Python第三方模块
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  
  1. 开启Pycharm,选择Create New Project。在编译环境Interpreter处选择:anaconda3相应的编译环境,点击Create。
  2. 在此项目下创建Python文件
# -*- 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()


  1. 右键运行WeiXinFX.py,会弹出一个二维码的网页,使用微信“扫一扫”功能,登录微信网页版后,关闭该二维码网页。等待程序运行完成,我们可以看到在文件夹下生成了render.html。找到render.html文件,双击打开。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Laura_Wangzx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值