用python统计微信好友

from wxpy import*
bot=Bot(cache_path=True)
friend_all=bot.friends()
lis=[] 


 
for a_friend in friend_all:     
    NickName = a_friend.raw.get('NickName',None)     
    #Sex = a_friend.raw.get('Sex',None)     
    Sex ={1:"",2:"",0:"其它"}.get(a_friend.raw.get('Sex',None),None)     
    City = a_friend.raw.get('City',None)     
    Province = a_friend.raw.get('Province',None)     
    Signature = a_friend.raw.get('Signature',None)     
    HeadImgUrl = a_friend.raw.get('HeadImgUrl',None)     
    HeadImgFlag  = a_friend.raw.get('HeadImgFlag',None)     
    list_0=[NickName,Sex,City,Province,Signature,HeadImgUrl,HeadImgFlag] 
    lis.append(list_0) 
def lis2e07(filename,lis):     
    '''     将列表写入 07 版 excel 中,其中列表中的元素是列表.     filename:保存的文件名(含路径)     lis:元素为列表的列表,如下:     lis = [["名称", "价格", "出版社", "语言"],              ["暗时间", "32.4", "人民邮电出版社", "中文"],              ["拆掉思维里的墙", "26.7", "机械工业出版社", "中文"]]     '''    
    import openpyxl     
    wb = openpyxl.Workbook()     
    sheet = wb.active    
    sheet.title = 'list2excel07'    
    file_name = filename +'.xlsx'    
    for i in range(0, len(lis)):        
        for j in range(0, len(lis[i])):          
            sheet.cell(row=i+1, column=j+1, value=str(lis[i][j]))  
    wb.save(file_name)     
    print("写入数据成功!")
#lis2e07('yu',lis) 

Friends = bot.friends() 
data = Friends.stats_text(total=True, sex=True,top_provinces=30, top_cities=500) 
print(data)


from pandas import read_excel  
df = read_excel('yu.xlsx',sheetname='list2excel07')  
df.tail(5) 
df.city.count() 
df.city.describe() 
from wordcloud import WordCloud  
import matplotlib.pyplot as plt 
import pandas as pd 
from pandas import DataFrame
word_list= df['city'].fillna('0').tolist()#将 dataframe 的列转化为 list,其中的 nan 用“0”替换 
new_text = ' '.join(word_list)  
wordcloud = WordCloud(font_path='simhei.ttf',  background_color="black").generate(new_text)  
plt.imshow(wordcloud)  
plt.axis("off")  
plt.show()  
心结 共有 162 位微信好友

男性: 67 (41.4%)
女性: 75 (46.3%)

TOP 30 省份
广东: 58 (35.80%)
山西: 28 (17.28%)
西藏: 2 (1.23%)
内蒙古: 2 (1.23%)
Barcelona: 1 (0.62%)
Wong Tai Sin: 1 (0.62%)
河北: 1 (0.62%)
山东: 1 (0.62%)
Daejeon: 1 (0.62%)
浙江: 1 (0.62%)
湖北: 1 (0.62%)
河南: 1 (0.62%)
Paris: 1 (0.62%)
Meath: 1 (0.62%)
北京: 1 (0.62%)
Bolivar: 1 (0.62%)
福建: 1 (0.62%)
辽宁: 1 (0.62%)
Bahia Blanca: 1 (0.62%)
Tipperary: 1 (0.62%)
贵州: 1 (0.62%)
Amsterdam: 1 (0.62%)

TOP 500 城市
大同: 22 (13.58%)
广州: 20 (12.35%)
深圳: 6 (3.70%)
汕头: 4 (2.47%)
河源: 3 (1.85%)
揭阳: 3 (1.85%)
梅州: 3 (1.85%)
朔州: 3 (1.85%)
拉萨: 2 (1.23%)
佛山: 2 (1.23%)
云浮: 2 (1.23%)
中山: 2 (1.23%)
肇庆: 2 (1.23%)
湛江: 2 (1.23%)
太原: 2 (1.23%)
阳江: 2 (1.23%)
潮州: 1 (0.62%)
东莞: 1 (0.62%)
呼和浩特: 1 (0.62%)
青岛: 1 (0.62%)
惠州: 1 (0.62%)
Daejeon: 1 (0.62%)
宁波: 1 (0.62%)
鄂州: 1 (0.62%)
珠海: 1 (0.62%)
茂名: 1 (0.62%)
三门峡: 1 (0.62%)
韶关: 1 (0.62%)
清远: 1 (0.62%)
朝阳: 1 (0.62%)
赤峰: 1 (0.62%)
宁德: 1 (0.62%)
抚顺: 1 (0.62%)
贵阳: 1 (0.62%)
忻州: 1 (0.62%)

原文地址:https://www.cnblogs.com/wxyzzj/p/10994814.html