Python编程实现查看微信上信息(运用itchat模块)、统计字符个数
程序员文章站
2022-07-05 20:01:19
...
一、Pycharm的安装
建议官网下载,安装最新版本,安装好之后安装itchat库
在Linux中安装pycharm时,先解压安装包,然后在解压好的文件夹中找到./bin/pycharm.sh 运行该文件进行安装
二、运用itchat模块.查看微信上的好友和好友性别比例
# 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))
三、运用itchat模块.查看微信上的好友和好友省份分布
# 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))
四、统计字符个数
统计"hello world hello python hello java hello c hello c"每个字母出现的次数,并找出出现频率最高的三个字符
from collections import Counter string = 'hello world hello python hello java hello c hello c' string_dict = {} for c in string: if c in string_dict: string_dict[c] = string_dict[c] + 1 else: string_dict[c] = 1 print(string_dict) counter = Counter(string) top_3_string = counter.most_common(3) print(top_3_string)