微信好友分析

一:使用到的库

1.itchat:初始化机器人

2.openpyxl:保存微信好友数据为excel表格

3.pyecharts:生成可视化的地图

4.wordcloud.matplotlib,jieba:生成词云图

二:
1.登陆网页版微信
import itchat
itchat.login()
friends=itchat.get_friends(update=True)[0:]

print(friends)获得好友的所有信息
print(len(friends[1:]获得微信好友人数

从上面的获取信息全字段来看,我们获取的每位好友的信息都是一个字典,字典里只有'City'、'Province'、'Signature'、
'NickName'、'HeadImgUrl'、'Sex'是我们需要的。下面我们就对这几个 key 进行提取。

lis=[] #创建一个空列表
for a_friend in friends[1:]:
    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)

将lis()保存到excel中

def list_excel(filename,lis):
    '''
    将列表写入excel中,其中列表中的元素是列表.
    filename:保存的文件名(含路径)
    lis:元素为列表的列表,如下:
    lis = [["名称", "价格", "出版社", "语言"],
    ["暗时间", "32.4", "人民邮电出版社", "中文"],
    ["拆掉思维里的墙", "26.7", "机械工业出版社", "中文"]]
    '''
    import openpyxl
    wb = openpyxl.Workbook()   #激活worksheet
    sheet = wb.active
    sheet.title = 'sheet1'     #创建一个表格
    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("写入数据成功!")
list_excel('yubg1',lis)

对数据进行简单的分析

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

猜你喜欢

转载自www.cnblogs.com/wjaihui/p/10961918.html
今日推荐