微信小项目

 微信好友分析

# -*- coding: utf-8 -*-
#导入模块
from wxpy import *
import openpyxl
import pandas as pd
from pyecharts import Map
from pyecharts import WordCloud
#初始化机器人,选择缓存模式(扫码)登录
bot=Bot(cache_path=True)
#获取我的所有微信好友信息
friend_all=bot.friends()
#print(friend_all[0].raw)friend_all[0]是我的微信昵称,.raw 则是获取我的全部信息
#获取微信好友信息存入列表
l=[]
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]
    l.append(list_0)
#粗略获取好友的统计信息
data=friend_all.stats_text(total=True,sex=True,top_provinces=30,top_cities=500)
#定义函数,把列表l存入filename命名的xlsx文件
def lte(filename,l):
    wb=openpyxl.Workbook()
    sheet=wb.active
    sheet.title='lte'
    file_name=filename+'.xlsx'
    for i in range(0,len(l)):
        for j in range(0,len(l[i])):
            sheet.cell(row=i+1,column=j+1,value=str(l[i][j]))
    wb.save(file_name)
#定义函数作出词云放入HTML文件中
def wth():
    df=pd.read_excel('text.xlsx')
    count=df.city.value_counts() #对 dataframe 进行全频率统计,排除了 nan
    city_list=df['city'].fillna('NAN').tolist()#将 dataframe 的列转化为 list,其中的 nan 用“NAN” 替换
    count_city=pd.value_counts(city_list)#对 list 进行全频率统计 
    name=count_city.index.tolist()
    value=count_city.tolist()
    wordcloud=WordCloud(width=1300,height=620)
    wordcloud.add("", name,value,word_size_range=[20,100])
    wordcloud.show_config()
    wordcloud.render('wcl.html')
#将这些好友在全国地图上做分布并存入HTML文件中
def ltc():
    df=pd.read_excel('text.xlsx')
    province_list=df['province'].fillna('NAN').tolist()#将dataframe的列转化为 list,其中的 nan 用 “NAN”替换
    count_province=pd.value_counts(province_list)#对 list 进行全频率统计
    value=count_province.tolist()
    attr=count_province.index.tolist()
    map=Map("各省微信好友分布",width=1200,height=600)
    map.add("",attr,value,maptype='china',is_visualmap=True,visual_text_color='#000',is_label_show=True)#显示地图上的省份
    map.show_config()
    map.render('map.html')

lte('text',l)#把好友信息存入excel文件
wth()#绘制词云

ltc()#绘制地图

猜你喜欢

转载自www.cnblogs.com/guyuanlin/p/10964324.html