Python(十)

微信用户数据分析

微信接口itchat

  • 第三方开源的微信个人接口
  • 安装方法:pip install itchat

用户数据分析指标

  • 不同性别好友人数和占比

实例1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
'''
功能:微信好友性别分析报表
'''
import itchat

def wechat_user_gender_report():
itchat.login()
friends = itchat.get_friends()
male_count = 0
female_count = 0
other_count = 0
for friend in friends[1:]:
gender = friend["Sex"]
if gender == 1:
male_count += 1
elif gender == 2:
female_count += 1
else:
other_count += 1
total = len(friends[1:]) #含有总数
print("------------*微信好友分析报告*----------------")
print("好友总数:{}".format(total))
print("男性好友数:%d,占比:%.2f%%"%(male_count,float(male_count)/total*100))
print("女性好友数:%d,占比:%.2f%%"%(female_count,float(female_count)/total*100))
print("未知性别好友数:%d,占比:%.2f%%" % (other_count, float(other_count) / total * 100))
wechat_user_gender_report()
  • 不同地域用户分布情况

实例2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
'''
功能:微信好友地域分布分析报表
'''
import itchat

def wechat_user_location_report():
itchat.login()
friends = itchat.get_friends()
province_dict = {}
for friend in friends[1:]:
province = friend["Province"]
if province == "":
province = "未知"
else:
province_dict[province] = province_dict.get(province,0) + 1

print(province_dict)
wechat_user_location_report()

绘图库matplotlib

  • Python的2D绘图库,可以绘制常用的直方图,折线图,散点图等
  • 安装方法:pip install matplotlib

链接:matplotlib官方

实例3:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
'''
功能:根据数据生成饼图
参数说明:
datas:展示的数据列表
labels:展示的数据标签
'''
def get_pie(self,datas,labels):
#设置字符集
plt.rcParams["font.sans-serif"] = ["SimHei"] ##设置字符集
plt.figure(figsize=(8,6),dpi=80) #创建图形的size(像素点),并且设置分辨率(dpi)
plt.axes(aspect= 1) #设置x轴和y轴的比例
plt.pie(datas,labels=labels,autopct="%.2f%%",shadow=False) # 设置饼图
plt.title("微信好友性别分析图")
plt.show()

def get_bar(self,datas,labels):
# 设置字符集
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.xlabel("province")
plt.ylabel("count")
plt.xticks(range(len(datas)),labels)
plt.bar(range(len(datas)),datas,color="rgb")
plt.title("微信好友地域分布图")
plt.show()

-------------本文结束感谢您的阅读-------------


本文标题:Python(十)

文章作者:HuXuzhe

发布时间:2018年07月09日 - 00:07

最后更新:2018年11月21日 - 15:11

原始链接:https://huxuzhe.github.io/2018/07/09/Python-十/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

HuXuzhe wechat
关注微信公众号:"AI数据分析算法",轻轻扫一扫,加入我们的AI_LEGENDS !
坚持原创,您的支持将鼓励我继续创作!
0%