第一个微信小项目

1. 使用到的库

① wxpy:初始化微信机器人

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

③ pyecharts:生成可视化的地图

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

特别提醒】:pyecharts 库用的是0.5.x版本,而在 pip 中安装的为1.x.x版本

2. 基本功能

① 分析微信好友数据

② 生成词云图

③ 生成地图展示

3. 代码实现

先从微信中下载信息并保存为html格式

    1. # -*- coding: utf-8 -*-  
    2. """ 
    3. Created on Wed Jun  5 13:02:31 2019 
    4.  
    5. @author: ASUS 
    6. """  
    7. from wxpy import *  
    8. import pandas as pd  
    9. bot = Bot(cache_path=True)  
    10. friend_all = bot.friends()  
    11. lis=[]  
    12. for a_friend in friend_all:  
    13.     NickName = a_friend.raw.get('NickName',None)  
    14.     Sex ={1:"男",2:"女",0:"其它"}.get(a_friend.raw.get('Sex',None),None)  
    15.     City = a_friend.raw.get('City',None)  
    16.     Province = a_friend.raw.get('Province',None)  
    17.     Signature = a_friend.raw.get('Signature',None)  
    18.     list_0=[NickName,Sex,City,Province,Signature]  
    19.     lis.append(list_0)  
    20. def toex(lis):  
    21.     text=pd.DataFrame(lis,columns=['微信名','性别','城市','省份','个性签名'])  
    22.     text.to_excel('wx.xlsx',encoding='\U0001f31a')  
    23.     print(1)  
    24. toex(lis)  

 

显示结果如下:

 

 

 利用获得的数据制造词云

  1. # -*- coding: utf-8 -*-  
  2. """ 
  3. Created on Wed Jun  5 14:07:47 2019 
  4.    
  5. @author: ASUS 
  6. """  
  7.     
  8. import pandas as pd  
  9. from pyecharts import WordCloud   
  10. df=pd.read_excel('wx.xlsx')  
  11. city_list = df['城市'].fillna('city').tolist()  
  12. count_city = pd.value_counts(city_list)  
  13. name = count_city.index.tolist()  
  14. value = count_city.tolist()  
  15. wordcloud=WordCloud(width=1300, height=620)  
  16. wordcloud.add("", name, value, word_size_range=[20, 100])  
  17. wordcloud.show_config()  
  18. wordcloud.render(r'wxcity.html')  
  19. print(1)  

 

用pyecharts制作地图:

 

  1. # -*- coding: utf-8 -*-  
  2. """ 
  3. Created on Wed Jun  5 14:08:21 2019 
  4.    
  5. @author: ASUS 
  6. """  
  7.     
  8. import pandas as pd  
  9. from pyecharts import Map   
  10. df=pd.read_excel('wx.xlsx')  
  11. pr_list = df['省份'].fillna('pr').tolist()  
  12. count_pr = pd.value_counts(pr_list)  
  13. attr =count_pr.index.tolist()   
  14. value = count_pr.tolist()  
  15. maap=Map("各省微信好友分布", width=1200, height=600)  
  16. maap.add("", attr, value, maptype='china', is_visualmap=True,visual_text_color='#000', is_label_show = True)  
  17. maap.show_config()  
  18. maap.render(r'wxpr.html')  
  19. print(1)  

显示效果:

 

posted @ 2019-06-05 23:49  艾尔最后的巨像  阅读(155)  评论(0编辑  收藏  举报