Micro-channel buddy data analysis

1. Using the library

① wxpy: micro-channel initialization robot

② openpyxl: micro-channel friend save data to an Excel spreadsheet

③ pyecharts: generating a visual map

④ wordcloud, matplotlib, jieba: generate word cloud

2. Basic functions

① micro-channel buddy data analysis

② generate word cloud

③ generate a map showing

  

Data analysis friend

Implementation code:

Coding = UTF-# 8 

# landing introduction of micro-channel interfaces 
from wxpy Import * 

# obtain two-dimensional code login 
BOT = Bot (cache_path = True) 

# micro-letter friends get basic data 
friend_all = bot.friends () 

# establish a two-dimensional list, basic friend information stored 
LIS = [[ ' the NickName ' , ' Sex ' , ' City ' , ' Province ' , ' the Signature ' , ' HeadImgUrl ' , ' HeadImgFlag ' ]] 
# save the data characteristic list 
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("读写数据成功")

 My buddy data micro letter as follows:

# 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()

效果图

 

 

将好友城市信息生成词云

实现代码如下

Guess you like

Origin www.cnblogs.com/hzxxxb/p/10978273.html