前言
提到编程语言,很多人都会产生误解,比如提到C++就会联想到算法、竞赛,提到Python就会联想到人工智能......,实际上,编程语言只是一种可以和计算机交互的口令,可以利用计算机的强大计算能力来简化我们的工作,服务于我们的生活。本文就是利用Python的wxpy模块来操作微信,批量获取信息、处理信息、可视化信息。
wxpy环境搭建
What is wxpy?
wxpy 在 itchat 的基础上,通过大量接口优化提升了模块的易用性,并进行丰富的功能扩展。
How to install wxpy?
Mac or Ubuntu:
sudo pip3 install wxpy
Windows:
pip install wxpy
初识wxpy
登录微信
import wxpy
bot = wxpy.Bot()
扫描二维码,即可登陆你的微信。

获取好友
my_friends = bot.friends()
for friend in my_friends():
print(friend)


统计好友性别
sex_dict = {'male': 0, 'female': 0, 'other': 0}
for friend in my_friends:
if friend.sex == 1:
sex_dict['male'] += 1
elif friend.sex == 2:
sex_dict['female'] += 1
else:
sex_dict['other'] += 1
print(sex_dict)
print("Total numbers of my friends: ", len(my_friends))

统计好友省份
province_dict = {
'北京': 0, '上海': 0, '天津': 0, '重庆': 0, '西藏': 0,
'河北': 0, '山西': 0, '吉林': 0, '辽宁': 0, '黑龙江': 0,
'陕西': 0, '甘肃': 0, '青海': 0, '山东': 0, '福建': 0,
'浙江': 0, '台湾': 0, '河南': 0, '湖北': 0, '湖南': 0,
'江西': 0, '江苏': 0, '安徽': 0, '广东': 0, '海南': 0,
'四川': 0, '贵州': 0, '云南': 0, '香港': 0, '澳门': 0,
'内蒙古': 0, '新疆': 0, '宁夏': 0, '广西': 0, '其他': 0,
}
for friend in my_friends:
if friend.province in province_dict.keys():
province_dict[friend.province] += 1
else:
province_dict['其他'] += 1
print(province_dict)

完整代码
import wxpy
bot = wxpy.Bot()
my_friends = bot.friends()
sex_dict = {'male': 0, 'female': 0, 'other': 0}
for friend in my_friends:
if friend.sex == 1:
sex_dict['male'] += 1
elif friend.sex == 2:
sex_dict['female'] += 1
else:
sex_dict['other'] += 1
print(sex_dict)
print("Total numbers of my friends: ", len(my_friends))
province_dict = {
'北京': 0, '上海': 0, '天津': 0, '重庆': 0, '西藏': 0,
'河北': 0, '山西': 0, '吉林': 0, '辽宁': 0, '黑龙江': 0,
'陕西': 0, '甘肃': 0, '青海': 0, '山东': 0, '福建': 0,
'浙江': 0, '台湾': 0, '河南': 0, '湖北': 0, '湖南': 0,
'江西': 0, '江苏': 0, '安徽': 0, '广东': 0, '海南': 0,
'四川': 0, '贵州': 0, '云南': 0, '香港': 0, '澳门': 0,
'内蒙古': 0, '新疆': 0, '宁夏': 0, '广西': 0, '其他': 0,
}
for friend in my_friends:
if friend.province in province_dict.keys():
province_dict[friend.province] += 1
else:
province_dict['其他'] += 1
print(province_dict)
项目实战
将Python 获取的信息导出json格式,用js展示到网页上
从我的Github仓库clone源码

git clone git@github.com:yanpeng1314/wxpy_wechat.git

查看目录结构

运行main.py文件,并用微信扫描二维码
python3 main.py
启动本地服务器
python3 -m http.server
结果展示

网友评论