‘‘‘
爬取所有T信好友的信息
‘‘‘
import itchat
from pandas import DataFrame
itchat.login()
friends=itchat.get_friends(update=True)[0:]
def get_var(var):
variable=[]
for i in friends:
value=i[var]
variable.append(value)
return variable
NickName=get_var(‘NickName‘)
Sex=get_var(‘Sex‘)
Province=get_var(‘Province‘)
City=get_var(‘City‘)
Signature=get_var(‘Signature‘)
data={‘NickName‘:NickName,‘Sex‘:Sex,‘Province‘:Province,‘City‘:City,‘Signature‘:Signature}
frame=DataFrame(data)
frame.to_csv(‘data.csv‘,index=True,encoding="utf_8_sig")
计算微信好友男女比例:
import itchat
itchat.login()
friends=itchat.get_friends(update=True)[0:]
male=female=other=0
for i in friends[1:]:
sex=i[‘Sex‘]
if sex==1:
male+=1
elif sex==2:
female+=1
else:
other+=1
total=len(friends[1:])
malecol=round(float(male)/total*100,2)
femalecol=round(float(female)/total*100,2)
othercol=round(float(other)/total*100,2)
print(‘男性朋友:%.2f%%‘ %(malecol)+‘\n‘+‘女性朋友:%.2f%%‘ % (femalecol)+‘\n‘+‘性别不明的好友:%.2f%%‘ %(othercol))
print("显示图如下:")
itchat的学习路径:https://itchat.readthedocs.io/zh/latest/