Python itchat 详解

作者: tww844475003 分类: Python 发布时间: 2021-05-22 16:33

获取微信好友头像

import itchat

'''
获取微信好友头像
'''

# 登录微信
itchat.auto_login(hotReload=True)
# 获取微信好友列表
for friend in itchat.get_friends():
    # print(friend)
    # print(friend['RemarkName'], friend(['NickName'], friend['Sex']))
    img = itchat.get_head_img(userName=friend['UserName'])
    path = 'D:\python\study\itchat\head_img\\' + friend['UserName'] + '.jpg'
    with open(path, 'wb') as f:
        f.write(img)

itchat.run()

统计好友的男女比例

import itchat

itchat.auto_login()

# 1.统计好友的男女比例
# 说明:friends是一个字典
friends = itchat.get_friends(hotReload=True)
info = {}

# 列表切片[1,2,3,4] => [1:] => [2,3,4]
for friend in friends[1:]:
    # 男
    if friend['Sex'] == 1:
        info['male'] = info.get('male', 0) + 1
    # 女
    elif friend['Sex'] == 2:
        info['female'] = info.get('female', 0) + 1
    # 性别未知
    else:
        info['other'] = info.get('other', 0) + 1
print(info)

前端开发那点事
微信公众号搜索“前端开发那点事”

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注