Python crawls the information of all WeChat friends

'''
Crawl the information of all T letter friends
'''
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")

Calculate the ratio of male and female WeChat friends:

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 ( ' Male friend: %.2f%% ' %(malecol)+ ' \n ' + ' Female friend: %.2f%% ' % (femalecol )+ ' \n ' + ' Friends of unknown gender: %.2f%% ' % (othercol))
 print ( "The display is as follows: " )

Learning path for itchat: https://itchat.readthedocs.io/en/latest/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325107171&siteId=291194637
Recommended