Linux自动化运维——Python(6)(itchat的运用 如何通过Python编程实现查看微信上的各种信息 统计字符个数)
程序员文章站
2022-03-17 14:01:40
...
1.pycharm的安装
首先需要安装pycharm ,安装好之后安装itchat库
在liunx系统中安装pycharm需要注意,首先解压安装包 解压后找到/.pycharm.sh 运行该文件 进行安装
2运用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))
3.运用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))
4.统计"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)