hzxxxb

1. 使用到的库

① wxpy:初始化微信机器人

② openpyxl:保存微信好友数据为Excel表格

③ pyecharts:生成可视化的地图

④ wordcloud、matplotlib、jieba:生成词云图

2. 基本功能

① 分析微信好友数据

② 生成词云图

③ 生成地图展示

  

分析好友数据

实现代码:

# coding = utf-8

#引入微信登陆接口
from wxpy import *

#获取登录二维码
bot = Bot(cache_path = True)

#获取微信朋友的基本数据
friend_all = bot.friends()

#建立一个二维列表,存储基本好友信息
lis = [[\'NickName\',\'Sex\',\'City\',\'Province\',\'Signature\',\'HeadImgUrl\',\'HeadImgFlag\']]
#把好有特征数据保存为列表
for a_friend in friend_all:
    NickName = a_friend.raw.get(\'NickName\', 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)

#把列表转换为 xlsx 表格
def list_to_xlsx(filename, list):
    
    import openpyxl
    wb = openpyxl.Workbook()
    sheet = wb.active
    sheet.title = \'Friends\'
    file_name = filename + \'.xlsx\'
    for i in range(0, len(list)):
        for j in range(0, len(list[i])):
            sheet.cell(row = i+1, column = j+1, value = str(list[i][j]))
            
    wb.save(file_name)
    print("读写数据成功")

 我的微信好友数据如下:

# coding = utf-8

#引入微信登陆接口
from wxpy import *

#获取登录二维码
bot = Bot(cache_path = True)

#获取微信朋友的基本数据
friend_all = bot.friends()

#建立一个二维列表,存储基本好友信息
lis = [[\'NickName\',\'Sex\',\'City\',\'Province\',\'Signature\',\'HeadImgUrl\',\'HeadImgFlag\']]
#把好有特征数据保存为列表
for a_friend in friend_all:
    NickName = a_friend.raw.get(\'NickName\', 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)
#把列表转换为 xlsx 表格
def list_to_xlsx(filename, list):
    
    import openpyxl
    wb = openpyxl.Workbook()
    sheet = wb.active
    sheet.title = \'Friends\'
    file_name = filename + \'.xlsx\'
    for i in range(0, len(list)):
        for j in range(0, len(list[i])):
            sheet.cell(row = i+1, column = j+1, value = str(list[i][j]))
            
    wb.save(file_name)
    print("读写数据成功")
    \'\'\'

#把列表生成表格
list_to_xlsx(\'wechat\', lis)
    
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import pandas as pd
from pandas import read_excel
import numpy as np

df = read_excel(\'wechat.xlsx\')

#使用 WordCloud 生成词云
word_list = df[\'City\'].fillna(\'0\').tolist()
new_text = \' \'.join(word_list)
wordcloud = WordCloud(font_path=\'msyh.ttc\', background_color = \'white\').generate(new_text)
plt.imshow(wordcloud)
plt.axis("off")
plt.show()

效果图

 

 

将好友城市信息生成词云

实现代码如下

分类:

技术点:

相关文章: