import itchat 微信库
import os 文件下载库
一.微信请求函数
def get_friends():
friends = itchat.get_friends(update=True)
return friends # 返回的是微信好友的信息列表
二.统计好友个数函数
def get_friendsNum(friends):
num = 0
for i in friends:
num += 1
return num
三.统计好友男女生占比情况
def get_friendsex(friends):
sex = {'male':0,'female':0,'other':0}
for i in friends:
if i['Sex'] ==1:
sex['male'] += 1
elif i['Sex'] == 2:
sex['female'] += 1
else:
sex['other'] += 1
return sex
男女生的性别用数字1和2表示,1代表男生,2代表女生,如果为空,则说明该好友信息中未设置性别
四.获取所在城市函数
def get_friendLocation(friends):
location = []
for i in friends:
if i['City']:
location.append(i['City'])
elif i['Province']:
location.append(i['Province'])
else:
location.append(None)
return location
五.下载微信好友头像函数
def get_HeadImang(friends):
if 'picture' not in os.listdir():
os.mkdir('picture')
os.chdir('picture')
for i in friends:
img = itchat.get_head_img(userName= i['UserName']) # 获取好友的头像
with open(i['NickName'].replace('*','')+'.jpg','wb') as f:
print("正在下载好友:%s的头像:" % i['NickName']) # 好友的网民
f.write(img)
六.主函数main()
if __name__ == '__main__':
itchat.auto_login(hotReload=True) # 设置为turn,可以半小时内不用重新扫描二维码
friends = get_friends() # 微信好友的信息
number = get_friendsNum(friends) # 微信通讯录个数
sex = get_friendsex(friends) # 微信男女比例
#print("男性:%d,女性:%d,其他:%d" % (sex['male'],sex['female'],sex['other']))
location = get_friendLocation(friends)
datong = location.count("大同")
weifang = location.count("潍坊")
beijing = location.count("北京")
taiyuan = location.count("太原")
print("亲爱的:%s" % friends[0]['NickName'])
print("您的好友共{}位".format(number))
print("其中男生{}位,女生{}位,未标注性别者{}位".format(sex['male'],
sex['female'],sex['other']))
print("主要的好友归属地为: 大同{}位,潍坊{}位,北京{}位,太原{}位".format(datong,weifang,beijing,taiyuan))
get_HeadImang(friends)
因为好友归属地过于复杂繁乱,只是将本人所待过的城市好友进行统计,friends所返回的信息还有微信好友的网名和个性签名等等 觉得获取那么没什么用,需要的朋友可以根据get_HeadImang进行修改字典的键就可以获取到的
输出的统计结果
扫描二维码关注公众号,回复:
11300343 查看本文章
微信好友的头像也爬取下来啦
好像获取到了也没什么用,这是一件极其无聊的事情,我也不知道自个花了一下午都干了点啥 --- 哈哈哈哈