第一个微信小项目

一、微信好友分析

•    本次操作所需要的库:wxpy

               pyecharts

               wordcloud

               numpy

               PIL

               matplotlib

               pandas

列举出来的库可能有遗漏,在测试过程中如果缺少必要的库会提示,此时再安装,缺啥补啥。

安装方式同样是打开命令行窗口,输入"pip install name"(其中name为所需安装的库名称)。

有的同学可能是使用Anaconda编写程序,那么要打开的就不是命令行窗口了,而是跟Anaconda在同一目录的"Anaconda Prompt",见下图:

同样的也是输入"pip install name"(name为所要安装的库的名称)

•     代码分解:

1.获取好友信息

def get_friends_info(self): #获取好像信息,返回lis列表
  bot = Bot()
  lis = [['昵称', '备注名称', '性别', '城市', '省份', '个人签名']]  # 把信息存储为一个二维列表,添加头部信息
  friend_all = bot.friends()

  for a_friend in friend_all:
    NickName = a_friend.raw.get('NickName', None) #获取所有好友信息 raw表示获取全部信息
    RemarkName = a_friend.raw.get('RemarkName', 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, RemarkName, Sex, City, Province, Signature]
    lis.append(list_0)
  return lis

 

2.将好友信息输入excel表格

def friends_info_lis_to_excle(self):
  import openpyxl
  lis = self.get_friends_info()
  wb = openpyxl.Workbook()   sheet = wb.active   sheet.title = self.sheetname   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(self.filename)   print("excel保存成功")

 

3.创建字典供词云使用

def extract_data_as_two_lis(self, condition):
  df = read_excel(self.filename, sheet_name=self.sheetname)
  X_list = df[condition].fillna('未知').tolist()  # 把列转换为list,用'未知'替换Nan?
  counts = {}  # 创建字典
  for word in X_list:
    counts[word] = counts.get(word, 0) + 1  # 统计词频
  items = list(counts.items())  # 返回所有键值对
  items.sort(key=lambda x: x[1], reverse=True)  # 降序排序
  keylist = list()
  valueslist = list()
  for item in items:
    word, count = item
    #if word == '0':
      #word = "未知"
    keylist.append(word)  # 把词语降序word放进一个列表
    valueslist.append(count)
  return keylist, valueslist

 

4.制作词云

def city_wordcloud(self, save_name, condition):
        wordlist, giveup = self.extract_data_as_two_lis(condition)
        new_wordlist = list()
        for i in range(25):
            new_wordlist.append(wordlist[i])
        wl = ' '.join(wordlist)  # 把列表转换成str wl为str类型,所以需要转换
        cloud_mask = np.array(Image.open("love.jpg"))  # 词云的背景图,需要颜色区分度高
        wc = WordCloud(
            background_color="black",  # 背景颜色
            mask=cloud_mask,  # 背景图cloud_mask
            max_words=100,  # 最大词语数目
            font_path='simfang.ttf',  # 调用font里的simfang.tff字体
            height=500,  # 设置高度
            width=2600,  # 设置宽度
            max_font_size=1000,  # 最大字体号
            random_state=1000,  # 设置随机生成状态,即有多少种配色方案
        )
        myword = wc.generate(wl)  # 用 wl的词语 生成词云
        # 展示词云图
        plt.imshow(myword)
        plt.axis("off")
        #plt.show()
        try:
            wc.to_file(save_name + '.jpg')  # 把词云保存下当前目录(与此py文件目录相同)
        except:
            print("词云保存失败")

 

5.生成html柱状图

def pillar_picture(self, condition, render_name):
        from pyecharts.charts import Bar
        from pyecharts.globals import ThemeType
        from pyecharts import options as opts

        keylist, valueslist = self.extract_data_as_two_lis(condition)
        new_keylist = list()
        new_valueslist = list()
        for i in range(10):
            new_keylist.append(keylist[i])
            new_valueslist.append(valueslist[i])
        bar = Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
        # x轴 列表
        bar.add_xaxis(new_keylist)
        bar.add_yaxis("好友分布", new_valueslist)
        # render 会生成本地 HTML 文件,默认会在当前目录生成 城市分布柱状图.html 文件
        # 也可以传入路径参数,如 bar.render("mycharts.html")
        bar.render(render_name)
        print("pillar_picture图片保存成功!")

 

6.生成城市分布图

def map_picture(self, condition, picture_name, keylist, valueslist, Map_condition):
        from pyecharts import options as opts
        from pyecharts.charts import Map

  

def map_visualmap() -> Map:
  new_keylist = list()
  new_valueslist = list()
  if condition == '城市':
    for i in range(len(keylist)):
      # 列表处理,默认elsx里面的city没有'市'字,地图需要市字
      new_keylist.append(keylist[i] + '市')
      new_valueslist.append(valueslist[i])
  else:
    for i in range(len(keylist)):
      new_keylist.append(keylist[i])
      new_valueslist.append(valueslist[i])

      c = (
         Map()
                    .add("好友地图分布", [list(z) for z in zip(new_keylist, new_valueslist)], Map_condition)
                    .set_global_opts(
                    title_opts=opts.TitleOpts(title="Map-VisualMap"),
                    visualmap_opts=opts.VisualMapOpts(max_=100),
                )
          )
      try:
        c.render(picture_name)
      except:
          print("html地图图片创建失败")
      print('html地图图片保存成功')
    map_visualmap()#调用自己

 

def find_friends_in_condition(self, condition):
        df = pd.read_excel(self.filename, usecols=[0, 1, 3], names=None)  #不要列名
        df_li = df.values.tolist()
        name = list()
        for data in df_li:
            condition = condition
            if condition in data:
                name.append(data)
        self.map_picture()
        #print(len(name), name)
        return name

  

•    代码汇总:

# -*- coding: utf-8 -*-
"""
Created on Sun Jun  2 20:31:17 2019

@author: Regan_White_Lin       12
"""

from wxpy import *
from wordcloud import WordCloud
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
from pandas import read_excel
import pandas as pd

class  wechat_test():

    def __init__(self, filename, sheetname):
        self.filename = filename
        self.sheetname = sheetname

    def get_friends_info(self): #获取好像信息,返回lis列表
        bot = Bot()
        lis = [['昵称', '备注名称', '性别', '城市', '省份', '个人签名']]  # 把信息存储为一个二维列表,添加头部信息
        friend_all = bot.friends()

        for a_friend in friend_all:
            NickName = a_friend.raw.get('NickName', None) #获取所有好友信息 raw表示获取全部信息
            RemarkName = a_friend.raw.get('RemarkName', 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, RemarkName, Sex, City, Province, Signature]
            lis.append(list_0)
        return lis

    # 把lis信息写入到excle
    def friends_info_lis_to_excle(self):
        import openpyxl
        lis = self.get_friends_info()
        wb = openpyxl.Workbook()
        sheet = wb.active
        sheet.title = self.sheetname
        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(self.filename)
        print("excel保存成功")

    #参数为condition 词频,提取两个列表,condition 和 人数,降序列表
    def extract_data_as_two_lis(self, condition):
        df = read_excel(self.filename, sheet_name=self.sheetname)
        X_list = df[condition].fillna('未知').tolist()  # 把列转换为list,用'未知'替换Nan?
        counts = {}  # 创建字典
        for word in X_list:
            counts[word] = counts.get(word, 0) + 1  # 统计词频
        items = list(counts.items())  # 返回所有键值对
        items.sort(key=lambda x: x[1], reverse=True)  # 降序排序
        keylist = list()
        valueslist = list()
        for item in items:
            word, count = item
            #if word == '0':
                #word = "其他"
            keylist.append(word)  # 把词语降序word放进一个列表
            valueslist.append(count)
        return keylist, valueslist

    #参数为 save_name 自动添加jpg 创建X条件词云,
    def city_wordcloud(self, save_name, condition):
        wordlist, giveup = self.extract_data_as_two_lis(condition)
        new_wordlist = list()
        for i in range(25):
            new_wordlist.append(wordlist[i])
        wl = ' '.join(wordlist)  # 把列表转换成str wl为str类型,所以需要转换
        cloud_mask = np.array(Image.open("love.jpg"))  # 词云的背景图,需要颜色区分度高
        wc = WordCloud(
            background_color="black",  # 背景颜色
            mask=cloud_mask,  # 背景图cloud_mask
            max_words=100,  # 最大词语数目
            font_path='simfang.ttf',  # 调用font里的simfang.tff字体
            height=500,  # 设置高度
            width=2600,  # 设置宽度
            max_font_size=1000,  # 最大字体号
            random_state=1000,  # 设置随机生成状态,即有多少种配色方案
        )
        myword = wc.generate(wl)  # 用 wl的词语 生成词云
        # 展示词云图
        plt.imshow(myword)
        plt.axis("off")
        #plt.show()
        try:
            wc.to_file(save_name + '.jpg')  # 把词云保存下当前目录(与此py文件目录相同)
        except:
            print("词云保存失败")

    #参数为condition ,创建柱形html图片,
    def pillar_picture(self, condition, render_name):
        from pyecharts.charts import Bar
        from pyecharts.globals import ThemeType
        from pyecharts import options as opts

        keylist, valueslist = self.extract_data_as_two_lis(condition)
        new_keylist = list()
        new_valueslist = list()
        for i in range(10):
            new_keylist.append(keylist[i])
            new_valueslist.append(valueslist[i])
        bar = Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
        # x轴 列表
        bar.add_xaxis(new_keylist)
        bar.add_yaxis("好友分布", new_valueslist)
        # render 会生成本地 HTML 文件,默认会在当前目录生成 城市分布柱状图.html 文件
        # 也可以传入路径参数,如 bar.render("mycharts.html")
        bar.render(render_name)
        print("pillar_picture图片保存成功!")

    #参数为condition,picture_name, keylist, valueslist, Map_condition->china或者广东创建html地图图片,图片名字为城市分布图
    def map_picture(self, condition, picture_name, keylist, valueslist, Map_condition):
        from pyecharts import options as opts
        from pyecharts.charts import Map

        def map_visualmap() -> Map:
            new_keylist = list()
            new_valueslist = list()
            if condition == '城市':
                for i in range(len(keylist)):
                    # 列表处理,默认elsx里面的city没有'市'字,地图需要市字
                    new_keylist.append(keylist[i] + '市')
                    new_valueslist.append(valueslist[i])
            else:
                for i in range(len(keylist)):
                    new_keylist.append(keylist[i])
                    new_valueslist.append(valueslist[i])

            c = (
                Map()
                    .add("好友地图分布", [list(z) for z in zip(new_keylist, new_valueslist)], Map_condition)
                    .set_global_opts(
                    title_opts=opts.TitleOpts(title="Map-VisualMap"),
                    visualmap_opts=opts.VisualMapOpts(max_=100),
                )
            )
            try:
                c.render(picture_name)
            except:
                print("html地图图片创建失败")
            print('html地图图片保存成功')
        map_visualmap()#调用自己

    #参数为condition,返回一个二维列表
    def find_friends_in_condition(self, condition):
        df = pd.read_excel(self.filename, usecols=[0, 1, 3], names=None)  #不要列名
        df_li = df.values.tolist()
        name = list()
        for data in df_li:
            condition = condition
            if condition in data:
                name.append(data)
        self.map_picture()
        #print(len(name), name)
        return name


if __name__ == "__main__":
    wechat = wechat_test('微信好友信息.xlsx', 'list')
    wechat.friends_info_lis_to_excle()
    wechat.city_wordcloud('城市词云', '城市')
    wechat.pillar_picture('城市', '城市分布柱状图.html')
    keylist, valueslist = wechat.extract_data_as_two_lis('城市')
    wechat.map_picture('城市', '城市分布图.html', keylist, valueslist, "广东")
    #wechat.find_friends_in_city()

 

效果如下:

1.好友数据提取(涉及个人隐私问题,做了点小动作)

 

 

2.城市分布词云展示

 

3.好友城市分布地图

 

4.好友城市分布柱形图

 

效果还是挺不错的。

posted @ 2019-06-02 21:01  Regan_White_Lin  阅读(297)  评论(0编辑  收藏  举报