1.用 Python 實現微信好友性別及位置信息統計
這裏使用的python3+wxpy庫+Anaconda(Spyder)開發。若是你想對wxpy有更深的瞭解請查看:wxpy: 用 Python 玩微信html
# -*- coding: utf-8 -*- """ 微信好友性別及位置信息 """ #導入模塊 from wxpy import Bot '''Q 微信機器人登陸有3種模式, (1)極簡模式:robot = Bot() (2)終端模式:robot = Bot(console_qr=True) (3)緩存模式(可保持登陸狀態):robot = Bot(cache_path=True) ''' #初始化機器人,選擇緩存模式(掃碼)登陸 robot = Bot(cache_path=True) #獲取好友信息 robot.chats() #robot.mps()#獲取微信公衆號信息 #獲取好友的統計信息 Friends = robot.friends() print(Friends.stats_text())
效果圖(來自筆主盆友圈):python
2.用 Python 實現聊天機器人
這裏使用的python3+wxpy庫+Anaconda(Spyder)開發。須要提早去圖靈官網建立一個屬於本身的機器人而後獲得apikey。git
- 使用圖靈機器人自動與指定好友聊天
讓室友幫忙測試發現發送表情發送文字還能迴應,可是發送圖片可能不會回覆,猜應該是咱們申請的圖靈機器人是最初級的沒有加圖片識別功能。github
# -*- coding: utf-8 -*- """ Created on Tue Mar 13 19:09:05 2018 @author: Snailclimb @description使用圖靈機器人自動與指定好友聊天 """ from wxpy import Bot,Tuling,embed,ensure_one bot = Bot() my_friend = ensure_one(bot.search('鄭凱')) #想和機器人聊天的好友的備註 tuling = Tuling(api_key='你申請的apikey') @bot.register(my_friend) # 使用圖靈機器人自動與指定好友聊天 def reply_my_friend(msg): tuling.do_reply(msg) embed()
- 使用圖靈機器人羣聊
# -*- coding: utf-8 -*- """ Created on Tue Mar 13 18:55:04 2018 @author: Administrator """ from wxpy import Bot,Tuling,embed bot = Bot(cache_path=True) my_group = bot.groups().search('羣聊名稱')[0] # 記得把名字改爲想用機器人的羣 tuling = Tuling(api_key='你申請的apikey') # 必定要添加,否則實現不了 @bot.register(my_group, except_self=False) # 使用圖靈機器人自動在指定羣聊天 def reply_my_friend(msg): print(tuling.do_reply(msg)) embed()
3.用 Python分析朋友圈好友性別分佈(圖標展現)
這裏沒有使用wxpy而是換成了Itchat操做微信,itchat只須要2行代碼就能夠登陸微信。若是你想詳細瞭解itchat,請查看:
itchat入門進階教程以及
itchat github項目地址
另外就是須要用到python的一個畫圖功能很是強大的第三方庫:matplotlib。
若是你想對matplotlib有更深的瞭解請查看個人博文:Python第三方庫matplotlib(詞雲)入門與進階面試
# -*- coding: utf-8 -*- """ Created on Tue Mar 13 17:09:26 2018 @author: Snalclimb @description 微信好友性別比例 """ import itchat import matplotlib.pyplot as plt from collections import Counter itchat.auto_login(hotReload=True) friends = itchat.get_friends(update=True) sexs = list(map(lambda x: x['Sex'], friends[1:])) counts = list(map(lambda x: x[1], Counter(sexs).items())) labels = ['Male','FeMale', 'Unknown'] colors = ['red', 'yellowgreen', 'lightskyblue'] plt.figure(figsize=(8, 5), dpi=80) plt.axes(aspect=1) plt.pie(counts, # 性別統計結果 labels=labels, # 性別展現標籤 colors=colors, # 餅圖區域配色 labeldistance=1.1, # 標籤距離圓點距離 autopct='%3.1f%%', # 餅圖區域文本格式 shadow=False, # 餅圖是否顯示陰影 startangle=90, # 餅圖起始角度 pctdistance=0.6 # 餅圖區域文本距離圓點距離 ) plt.legend(loc='upper right',) plt.title('%s的微信好友性別組成' % friends[0]['NickName']) plt.show()
效果圖(來自筆主盆友圈):api
4.用 Python分析朋友圈好友的簽名
須要用到的第三方庫:緩存
numpy:本例結合wordcloud使用微信
jieba:對中文驚進行分詞app
PIL: 對圖像進行處理(本例與wordcloud結合使用)dom
snowlp:對文本信息進行情感判斷
wordcloud:生成詞雲
matplotlib:繪製2D圖形
# -*- coding: utf-8 -*- """ 朋友圈朋友簽名的詞雲生成以及 簽名情感分析 """ import re,jieba,itchat import jieba.analyse import numpy as np from PIL import Image from snownlp import SnowNLP from wordcloud import WordCloud import matplotlib.pyplot as plt itchat.auto_login(hotReload=True) friends = itchat.get_friends(update=True) def analyseSignature(friends): signatures = '' emotions = [] for friend in friends: signature = friend['Signature'] if(signature != None): signature = signature.strip().replace('span', '').replace('class', '').replace('emoji', '') signature = re.sub(r'1f(\d.+)','',signature) if(len(signature)>0): nlp = SnowNLP(signature) emotions.append(nlp.sentiments) signatures += ' '.join(jieba.analyse.extract_tags(signature,5)) with open('signatures.txt','wt',encoding='utf-8') as file: file.write(signatures) # 朋友圈朋友簽名的詞雲相關屬性設置 back_coloring = np.array(Image.open('alice_color.png')) wordcloud = WordCloud( font_path='simfang.ttf', background_color="white", max_words=1200, mask=back_coloring, max_font_size=75, random_state=45, width=1250, height=1000, margin=15 ) #生成朋友圈朋友簽名的詞雲 wordcloud.generate(signatures) plt.imshow(wordcloud) plt.axis("off") plt.show() wordcloud.to_file('signatures.jpg')#保存到本地文件 # Signature Emotional Judgment count_good = len(list(filter(lambda x:x>0.66,emotions)))#正面積極 count_normal = len(list(filter(lambda x:x>=0.33 and x<=0.66,emotions)))#中性 count_bad = len(list(filter(lambda x:x<0.33,emotions)))#負面消極 labels = [u'負面消極',u'中性',u'正面積極'] values = (count_bad,count_normal,count_good) plt.rcParams['font.sans-serif'] = ['simHei'] plt.rcParams['axes.unicode_minus'] = False plt.xlabel(u'情感判斷')#x軸 plt.ylabel(u'頻數')#y軸 plt.xticks(range(3),labels) plt.legend(loc='upper right',) plt.bar(range(3), values, color = 'rgb') plt.title(u'%s的微信好友簽名信息情感分析' % friends[0]['NickName']) plt.show() analyseSignature(friends)
效果圖(來自筆主盆友圈):
參考文獻:
itchat文檔:http://itchat.readthedocs.io/zh/latest/tutorial/tutorial0/
wxpy文檔:http://wxpy.readthedocs.io/zh/latest/index.html
基於Python實現的微信好友數據分析(簽名,頭像等等):
http://blog.csdn.net/qinyuanpei/article/details/79360703
Windows環境下Python中wordcloud的使用——本身踩過的坑 :http://blog.csdn.net/heyuexianzi/article/details/76851377
歡迎關注個人微信公衆號:「Java面試通關手冊」(堅持原創,分享美文,分享各類Java學習資源,面試題,以及企業級Java實戰項目回覆關鍵字免費領取):
github項目地址(系列文章包含常見第三庫的使用與爬蟲,會持續更新)歡迎star和fork.