微信小项目

一、所需要的七个第三方库及其安装

1、Pillow

PIL:Python Imaging Library,已经是 Python 平台事实上的图像处理标准库。PIL功能非常强大,但API却非常简单易用。

如果安装了Anaconda,Pillow就已经可用了。否则,需要在命令行下通过pip安装:

pip install pillow

如果遇到Permission denied安装失败,请加上sudo重试。

2、Pyecharts

是一个用于生成 Echarts 图表的类库。Echarts 是百度开源的一个数据可视化 JS 库。用 Echarts 生成的图可视化效果非常棒,pyecharts 是为了与 Python 进行对接,方便在 Python 中直接使用数据生成图。使用pyecharts可以生成独立的网页,也可以在flask、django中集成使用。

可在命令行通过pip安装:

pip install pyecharts 

3、Itchat

itchat 是一个开源的微信个人号接口,使用 python 调用微信从未如此简单。

可在在命令行通过pip安装:

pip install itchat

4、Jieba

Jieba库是一款优秀的 Python 第三方中文分词库,jieba 支持三种分词模式:精确模式、全模式和搜索引擎模式,下面是三种模式的特点。

精确模式:试图将语句最精确的切分,不存在冗余数据,适合做文本分析

全模式:将语句中所有可能是词的词语都切分出来,速度很快,但是存在冗余数据

搜索引擎模式:在精确模式的基础上,对长词再次进行切分

可在在命令行通过pip安装:

pip install jieba

5、Numpy

NumPy 是一个 Python 的第三方库,代表 “Numeric Python”,主要用于数学/科学计算。 它是一个由多维数组对象和用于处理数组的例程集合组成的库。

使用 Numpy 我们可以轻松进行如下等计算:

  • 数组的算数和逻辑运算。
  • 傅立叶变换和用于图形操作的例程。
  • 与线性代数有关的操作。 NumPy 拥有线性代数和随机数生成的内置函数。

可在在命令行通过pip安装:

pip install Numpy

6、Pandas

Pandas 是基于 NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。Pandas纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具。pandas 提

供了大量能使我们快速便捷地处理数据的函数和方法。你很快就会发现,它是使 Python 成为强大而高效的数据分析环境的重要因素之一。

可在在命令行通过pip安装:

pip install Pandas

7、wxpy

wxpy 在 itchat 的基础上,通过大量接口优化提升了模块的易用性,并进行丰富的功能扩展。

可在在命令行通过pip安装:

pip install wxpy

8、安装地图数据包

pip install echarts-china-provinces-pypkg
pip install echarts-countries-pypkg  

 

二.微信操作

1.登陆微信

from wxpy import *
#初始化机器人,选择缓存模式(扫码)登录
bot = Bot(cache_path=True)
#获取我的所有微信好友信息
friend_all = bot.friends()

 

2.获取好友信息然后存为excel文件再进行好友分析

 

# -*- coding: utf-8 -*-
"""
Created on Sun Jun  2 22:10:54 2019

@author: lzz
"""

from wxpy import *
#初始化机器人,选择缓存模式(扫码)登录
bot = Bot(cache_path=True)
#获取我的所有微信好友信息
friend_all = bot.friends()
print(friend_all[0].raw)#获取自己的全部信息
print(len(friend_all))#输出自己的微信好友人数
lis=[] #创建一个空列表
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]
    lis.append(list_0)
def lis2e07(filename,lis):
    import openpyxl
    wb = openpyxl.Workbook()
    sheet = wb.active
    sheet.title = 'list2excel07'
    file_name = filename +'.xlsx'
    for i in range(len(lis)):
        for j in range(len(lis[i])):
            sheet.cell(row=i+1, column=j+1, value=str(lis[i][j]))
    wb.save(file_name)
lis2e07('H:\python程序\weixin',lis)
Friends = bot.friends()
data = Friends.stats_text(total=True, sex=True,top_provinces=30, top_cities=500)
print(data)

 

 

 

 

 

 

 把微信好友分布变成云图

#  导入模块
from wxpy import Bot
import openpyxl
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import jieba
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import pandas as pd
from pandas import DataFrame
#初始化机器人,选择缓存模式(扫码)登录
bot = Bot(cache_path=True)
#获取我的所有微信好友信息
friend_all = bot.friends()
print(friend_all[0].raw)#获取自己的全部信息
print(len(friend_all))#输出自己的微信好友人数


lis=[] #创建一个空列表
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]
    lis.append(list_0)

    
def lis2e07(filename,lis):
    infoTitle = ['NickName', 'Sex', 'Province', 'City']
    wb = openpyxl.Workbook()
    sheet = wb.active
    sheet.title = 'list2excel07'
    for _ in range(len(infoTitle)):
        sheet.cell(row=1, column=_+1, value=infoTitle[_])
    file_name = filename +'.xlsx'
    for i in range(len(lis)):a
        for j in range(len(infoTitle)):
            sheet.cell(row=i+2, column=j+1, value=str(lis[i][j]))
    wb.save(file_name)
lis2e07('H:\\python程序\\weixin1',lis)



from pandas import read_excel
df = read_excel('H:\\python程序\\weixin1.xlsx',sheet_name='list2excel07')
word_list= df['City'].fillna('0').tolist()#将 dataframe 的列转化为 list,其中的 nan 用“0”替换
new_text = ' '.join(word_list)
wordcloud = WordCloud(font_path='simhei.ttf', background_color="black").generate(new_text)
plt.imshow(wordcloud)
plt.axis("off")
plt.show()

 

posted @ 2019-06-02 22:41  落尘一缕沙  阅读(191)  评论(0编辑  收藏  举报