python练习之安装itchat以及分析微信好友信息

安装itchat包:

  • 方法一:
    python练习之安装itchat以及分析微信好友信息_第1张图片
    python练习之安装itchat以及分析微信好友信息_第2张图片
    python练习之安装itchat以及分析微信好友信息_第3张图片
    python练习之安装itchat以及分析微信好友信息_第4张图片
    此操作较为麻烦
  • 方法二:

python练习之安装itchat以及分析微信好友信息_第5张图片

[root@westos_netfilesystem day02]# pip3 install itchat

使用此命令安装比较慢:
python练习之安装itchat以及分析微信好友信息_第6张图片
建议指定由国内的 http://pypi.douban.com/simple安装(此为国内豆瓣提供的镜像源下载itchat包),使用该方法安装非常快:

[root@westos_netfilesystem day02]# pip3 install itchat -i http://pypi.douban.com/simple

python练习之安装itchat以及分析微信好友信息_第7张图片

1.分析微信好友信息:

# Alt + Insert  ==== Create New file
# Ctrl + Alt + S  ===== seetings
# python package: itchat
import itchat

# 1. Login weixin
itchat.auto_login(hotReload=True)

# 2. get friends information, return friends List [1, 2, 3, 4, .....]
# Every friend information saved as dict.
friends = itchat.get_friends()

# First friend information is yours
mine = friends[0]
name = mine['NickName']
Signature = mine['Signature']

# init male, female, other = 0
male = female = other = 0
for friend in friends[1:]:
    sex = friend['Sex']
    if sex == 1:
        male += 1
    elif sex == 2:
        female += 1
    else:
        other += 1
# Ctrl + D : Quick copy one line
print("""
*******************************weixin INformation***********************
        Name: %s
        Signature: %s
        Male friends count: %d 
        Female friends count: %d
        Unknown sex friends count: %d 

""" %(name, Signature, male, female, other))

python练习之安装itchat以及分析微信好友信息_第8张图片
python练习之安装itchat以及分析微信好友信息_第9张图片
python练习之安装itchat以及分析微信好友信息_第10张图片

python练习之安装itchat以及分析微信好友信息_第11张图片
python练习之安装itchat以及分析微信好友信息_第12张图片
python练习之安装itchat以及分析微信好友信息_第13张图片

2.分析微信好友省份信息

# 1. Analyse
# friend province
# "shanxi"-200  "beijing"-100  "shandong" 10 .........
# dict: {"province_name":people_num}

# 2.
import itchat
from collections import  Counter

# 3.
itchat.auto_login(hotReload=True)
# friends===List, friend===Dict {"NickName":"", "Sex":"", "Province":""}
# First Information is your info
friends = itchat.get_friends()

# First friend information is yours
mine = friends[0]
name = mine['NickName']
Signature = mine['Signature']


# 4.
# Save Province to dict
provinces = {}
for friend in friends[1:]:
    province = friend['Province']
    if province != '':
        # If province not in dict, set value=1
        # If province in dict, value += 1
        if province in provinces:
            # value = provinces[province]    # province people count
            # value += 1                     # value + 1
            # provinces[province] = value     # set new value
            provinces[province]  += 1
        else:
            provinces[province] = 1

# 5.
counter = Counter(provinces)
top_5_provinces = counter.most_common(5)

# 6.
print("""
*******************************weixin INformation***********************
        Name: %s
        Signature: %s
""" %(name, Signature))

print("*******************************weixin friends Province***********************")
for name, count in top_5_provinces:
    print("%s : %s" %(name, count))

你可能感兴趣的:(python)