第一個微信小項目

1. 使用到的庫 html

① wxpy:初始化微信機器人微信

② openpyxl:保存微信好友數據爲Excel表格app

③ pyecharts:生成可視化的地圖echarts

④ wordcloud、matplotlib、jieba:生成詞雲圖spa

特別提醒】:pyecharts 庫用的是0.5.x版本,而在 pip 中安裝的爲1.x.x版本excel

2. 基本功能 htm

① 分析微信好友數據blog

② 生成詞雲圖ip

③ 生成地圖展現utf-8

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)  

顯示效果:

相關文章
相關標籤/搜索