import itchat

itchat.login()
friends=itchat.get_friends(update=True)[0:]
male=female=other=0
for i in friends[1:]:
sex=i['Sex']
if sex==1:
male+=1
elif sex==2:
female+=1
else:
other+=1 total=len(friends[1:])
malecol=round(float(male)/total*100,2)
femalecol=round(float(female)/total*100,2)
othercol=round(float(other)/total*100,2)
print('男性朋友:%.2f%%' %(malecol)+'\n'+'女性朋友:%.2f%%' % (femalecol)+'\n'+'性别不明的好友:%.2f%%' %(othercol))
print("显示图如下:") import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
#解决中文乱码不显示问题
mpl.rcParams['font.sans-serif'] = ['SimHei'] #指定默认字体
mpl.rcParams['axes.unicode_minus'] = False #解决保存图像是负号'-'显示为方块的问题 map = {
'Female': (malecol, '#7199cf'),
'Male': (femalecol, '#4fc4aa'),
'other': (othercol, '#e1a7a2')
} fig = plt.figure(figsize=(5,5))# 整体图的标题
ax = fig.add_subplot(111)#添加一个子图
ax.set_title('Gender of friends') xticks = np.arange(3)+0.15# 生成x轴每个元素的位置
bar_width = 0.5# 定义柱状图每个柱的宽度
names = map.keys()#获得x轴的值
values = [x[0] for x in map.values()]# y轴的值
colors = [x[1] for x in map.values()]# 对应颜色 bars = ax.bar(xticks, values, width=bar_width, edgecolor='none')# 画柱状图,横轴是x的位置,纵轴是y,定义柱的宽度,同时设置柱的边缘为透明
ax.set_ylabel('Proprotion')# 设置标题
ax.set_xlabel('Gender')
ax.grid()#打开网格
ax.set_xticks(xticks)# x轴每个标签的具体位置
ax.set_xticklabels(names)# 设置每个标签的名字
ax.set_xlim([bar_width/2-0.5, 3-bar_width/2])# 设置x轴的范围
ax.set_ylim([0, 100])# 设置y轴的范围
for bar, color in zip(bars, colors):
bar.set_color(color)# 给每个bar分配指定的颜色
height=bar.get_height()#获得高度并且让字居上一点
plt.text(bar.get_x()+bar.get_width()/4.,height,'%.2f%%' %float(height))#写值
plt.show()
#画饼状图
fig1 = plt.figure(figsize=(5,5))# 整体图的标题
ax = fig1.add_subplot(111)
ax.set_title('Pie chart')
labels = ['{}\n{} %'.format(name, value) for name, value in zip(names, values)]
ax.pie(values, labels=labels, colors=colors)#并指定标签和对应颜色
plt.show() #用来爬去各个变量
def get_var(var):
variable=[]
for i in friends:
value=i[var]
variable.append(value)
return variable #调用函数得到各个变量,并把数据存到csv文件中,保存到桌面
NickName=get_var('NickName')
Sex=get_var('Sex')
Province=get_var('Province')
City=get_var('City')
Signature=get_var('Signature') pros=set(Province)#去重
prosarray=[]
for item in pros:
prosarray.append((item,Province.count(item)))#获取个数
def by_num(p):
return p[1]
prosdsored=sorted(prosarray,key=by_num,reverse=True)#根据个数排序 #画图
figpro = plt.figure(figsize=(10,5))# 整体图的标题
axpro = figpro.add_subplot(111)#添加一个子图
axpro.set_title('Province')
xticks = np.linspace(0.5,20,20)# 生成x轴每个元素的位置
bar_width = 0.8# 定义柱状图每个柱的宽度
pros=[]
values = []
count=0
for item in prosdsored:
pros.append(item[0])
values.append(item[1])
count=count+1
if count>=20:
break colors = ['#FFEC8B','#FFE4C4','#FFC125','#FFB6C1','#CDCDB4','#CDC8B1','#CDB79E','#CDAD00','#CD96CD','#CD853F','#C1FFC1','#C0FF3E','#BEBEBE','#CD5C5C','#CD3700','#CD2626','#8B8970','#8B6914','#8B5F65','#8B2252']# 对应颜色 bars = axpro.bar(xticks, values, width=bar_width, edgecolor='none')
axpro.set_ylabel('人数')# 设置标题
axpro.set_xlabel('省份')
axpro.grid()#打开网格
axpro.set_xticks(xticks)# x轴每个标签的具体位置
axpro.set_xticklabels(pros)# 设置每个标签的名字
axpro.set_xlim(0,20)# 设置x轴的范围
axpro.set_ylim([0, 100])# 设置y轴的范围 for bar, color in zip(bars, colors):
bar.set_color(color)# 给每个bar分配指定的颜色
height=bar.get_height()#获得高度并且让字居上一点
plt.text(bar.get_x()+bar.get_width()/4.,height,'%.d' %float(height))#写值 plt.show() #保存数据
from pandas import DataFrame
data={'NickName':NickName,'Sex':Sex,'Province':Province,'City':City,'Signature':Signature}
frame=DataFrame(data) frame.to_csv('data.csv',index=True,encoding="utf_8_sig")

 itchat的学习路径:https://itchat.readthedocs.io/zh/latest/

python实战===itchat的更多相关文章

  1. zeromq 学习和python实战

    参考文档: 官网 http://zeromq.org/ http://www.cnblogs.com/rainbowzc/p/3357594.html   原理解读 zeromq只是一层针对socke ...

  2. Python实战:美女图片下载器,海量图片任你下载

    Python应用现在如火如荼,应用范围很广.因其效率高开发迅速的优势,快速进入编程语言排行榜前几名.本系列文章致力于可以全面系统的介绍Python语言开发知识和相关知识总结.希望大家能够快速入门并学习 ...

  3. Python实战:Python爬虫学习教程,获取电影排行榜

    Python应用现在如火如荼,应用范围很广.因其效率高开发迅速的优势,快速进入编程语言排行榜前几名.本系列文章致力于可以全面系统的介绍Python语言开发知识和相关知识总结.希望大家能够快速入门并学习 ...

  4. python实战--数据结构二叉树

    此文将讲述如何用python实战解决二叉树实验 前面已经讲述了python语言的基本用法,现在让我们实战一下具体明确python的用法 点击我进入python速成笔记 先看一下最终效果图: 首先我们要 ...

  5. 再一波Python实战项目列表

    前言: 近几年Python可谓是大热啊,很多人都纷纷投入Python的学习中,以前我们实验楼总结过多篇Python实战项目列表,不但有用还有趣,最主要的是咱们实验楼不但有详细的开发教程,更有在线开发环 ...

  6. python实战:用70行代码写了一个山炮计算器!

    python实战训练:用70行代码写了个山炮计算器! 好了...好了...各位因为我是三年级而发牢骚的各位伙伴们,我第一次为大家插播了python的基础实战训练.这个,我是想给,那些python基础一 ...

  7. python实战博客

    2018-10-31 更新Logging日志记录以及异常捕获 感谢廖大教程.Python实战 直接在闲置的服务器上开发.阿里云Centos 6.8 64位. 1 搭建开发环境 Python 环境是Py ...

  8. python实战提升--1

    #python实战提升 1. 如何在列表.字典.集合中根据条件筛选数据? python中for _ in range(10)与for i in range(10)有何区别 下划线表示 临时变量, 仅用 ...

  9. 原创:centos7.1下 ZooKeeper 集群安装配置+Python实战范例

    centos7.1下 ZooKeeper 集群安装配置+Python实战范例 下载:http://apache.fayea.com/zookeeper/zookeeper-3.4.9/zookeepe ...

随机推荐

  1. 【bzoj1038】瞭望塔 半平面交

    题目描述 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 我们可以用一条山的上方轮廓折线(x1, y1), ( ...

  2. hdu 2108 Shape of HDU (数学)

    Shape of HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  3. NVIDIA TensorRT 让您的人工智能更快!

    NVIDIA TensorRT 让您的人工智能更快! 英伟达TensorRT™是一种高性能深度学习推理优化器和运行时提供低延迟和高通量的深度学习推理的应用程序.使用TensorRT,您可以优化神经网络 ...

  4. BZOJ1096:[ZJOI2007]仓库建设——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1096 L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于 ...

  5. NOIP2017金秋冲刺训练营杯联赛模拟大奖赛第二轮Day2题解

    肝了两题... T1一眼题,分解质因数,找出2的个数和5的个数取min输出 #include<iostream> #include<cstring> #include<c ...

  6. 高效率JavaScript代码的编写技巧

    使用DocumentFragment优化多次append 添加多个dom元素时,先将元素append到DocumentFragment中,最后统一将DocumentFragment添加到页面.该做法可 ...

  7. UVA10766:Organising the Organisation(生成树计数)

    Organising the Organisation 题目链接:https://vjudge.net/problem/UVA-10766 Description: I am the chief of ...

  8. opencv学习---打开摄像头检测个人头像

    opencv中具有检测人体各部分的级联分类器,在opencv文件夹里面的sources/data/haarcascades里面. 这里要选择的是能够检测人体头像的还有检测眼睛的级联分类器的文件. 它们 ...

  9. POJ2155 树状数组

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 26650   Accepted: 9825 Descripti ...

  10. SSH客户端,FinalShell服务器管理,远程桌面加速软件,支持Windows,Mac OS X,Linux,版本2.6.3.1

    FinalShell是一体化的的服务器,网络管理软件,不仅是ssh客户端,还是功能强大的开发,运维工具,充分满足开发,运维需求. 用户QQ群 342045988 Windows版下载地址:http:/ ...