Python使用微信itchat接口實現(xiàn)查看自己微信的信息功能詳解
本文實例講述了Python使用微信itchat接口實現(xiàn)查看自己微信的信息功能。分享給大家供大家參考,具體如下:
itchat是python的一個api,可以訪問自己的微信信息,功能還蠻好玩的,可以扒取朋友信息,自動回復短信等等。
package:
itchat1.3.10 + python3.5 + wordcloud1.4.1
登錄登出:
itchat.login() #hotReload設(shè)置為True,可以保持一段時間登錄 itchat.autologin(hotReload=True) itchat.logout()
獲取朋友數(shù)據(jù):
friends = itchat.get_friends(update=True)[0:]
搜索某個朋友:
itchat.search_friends(name='name') itchat.search_friends(wechatAccount='wechatid')
公眾號和群聊的獲取方式也是類似的:
itchat.get_mps(update=True)[0:] itchat.search_mps() itchat.get_chatrooms(update=True)[0:] itchat.search_chatroom()
發(fā)信息:
itchat.send(msg='Received Your Message',toUserName=userName]) #username其實是一個id,nickname是微信名字,remarkname是備注名
自動回復信息:
@itchat.msg_register(itchat.content.TEXT) def simple_reply(recv_msg): msg = recv_msg['Text'] if msg == 'name': itchat.send(msg=u'Received name from',toUserName=recv_msg['FromUserName']) elif msg == 'age': itchat.send(msg=u'Received age from',toUserName=recv_msg['FromUserName']) itchat.run() #register也接受其他參數(shù),比如說isGroupChat=True用來只自動回復群聊信息
register還可以注冊其他參數(shù):
MAP | 地理位置的分享 |
CARD | 名片信息 |
SHARING | 鏈接分享 |
PICTURE | 表情或照片 |
RECORDING | 語音 |
ATTACHMENT | 附件 |
VIDEO | 視頻 |
FRIENDS | 加好友申請,也就是說發(fā)起的一個好友申請其實是一條特殊的信息 |
SYSTEM | 系統(tǒng)消息,比如系統(tǒng)推送消息或者是某個群成員發(fā)生變動等等 |
NOTE | 通知文本,比如撤回了消息等等 |
例子:拉取朋友數(shù)據(jù),用wordcloud可視化朋友signature
先讀取數(shù)據(jù)
import itchat itchat.login() friends = itchat.get_friends(update=True)[0:]
簡單分析下性別比例
male = female = other = 0 #friends[0] is personal information, friends start from 1 for i in friends[1:]: sex = i["Sex"] if sex == 1: male += 1 elif sex == 2: female += 1 else: other +=1 total = len(friends[1:]) print("male: %.2f%%" % (float(male)/total*100) + "\n" + "female: %.2f%%" % (float(female) / total * 100) + "\n" + "unknown: %.2f%%" % (float(other) / total * 100))
獲得各個參數(shù),存入本地
filename = '' #需要修改這里 #爬取各個變量 def get_var(var): variable = [] for i in friends: value = i[var] variable.append(value) return variable #把數(shù)據(jù)存到csv文件中,保存到桌面 NickName = get_var("NickName") Sex = get_var('Sex') Province = get_var('Province') City = get_var('City') Signature = get_var('Signature') from pandas import DataFrame data = {'NickName': NickName, 'Sex': Sex, 'Province': Province, 'City': City, 'Signature': Signature} frame = DataFrame(data) frame.to_csv(filename, index=True)
去除特殊字符和轉(zhuǎn)義字符等
import re siglist = [] for i in friends: signature = i["Signature"].strip().replace("span","").replace("class","").replace("emoji","") rep = re.compile("1f\d+\w*|[<>/=]") signature = rep.sub("", signature) siglist.append(signature)
查看signature列表
#去掉末尾的空格以及空的簽名 while '' in siglist: siglist.remove('') for i in range(len(siglist)): siglist[i].strip() print(i,siglist[i]) #wordcloud讀取數(shù)據(jù)要求為string,以空格隔開 text = "".join(siglist)
可視化簽名
import matplotlib.pyplot as plt from wordcloud import WordCloud, ImageColorGenerator import numpy as np import PIL.Image as Image picture_path = '' #這里需要修改 coloring = np.array(Image.open(picture_path)) my_wordcloud = WordCloud(background_color="white", max_words=2000, font_path="2.ttf", mask = coloring, max_font_size=60, random_state=42, scale=2).generate(text) image_colors = ImageColorGenerator(coloring) plt.imshow(my_wordcloud.recolor(color_func=image_colors)) plt.imshow(my_wordcloud) plt.axis("off") plt.show()
保存:
save_path = '' #這里需要修改 my_wordcloud.to_file(save_path)
這里是以自己選的picture為形狀,生成詞云,以下是我的生成結(jié)果:
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)學運算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
Python數(shù)據(jù)分析之雙色球基于線性回歸算法預測下期中獎結(jié)果示例
這篇文章主要介紹了Python數(shù)據(jù)分析之雙色球基于線性回歸算法預測下期中獎結(jié)果,涉及Python基于線性回歸算法的數(shù)值運算相關(guān)操作技巧,需要的朋友可以參考下2018-02-02使用Python通過win32 COM打開Excel并添加Sheet的方法
今天小編就為大家分享一篇使用Python通過win32 COM打開Excel并添加Sheet的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05Python+OpenCV實戰(zhàn)之利用?K-Means?聚類進行色彩量化
這篇文章主要介紹了如何利用?K-Means?聚類進行色彩量化,以減少圖像中顏色數(shù)量。文中的代碼具有一定的學習價值,感興趣的小伙伴可以關(guān)注一下2021-12-12Python 實現(xiàn) 貪吃蛇大作戰(zhàn) 代碼分享
本文給大家分享的是一個使用cocos2d-python游戲引擎庫制作出來的貪吃蛇大作戰(zhàn)的游戲代碼,基于Python 2.7 和 cocos2d 庫,有需要的小伙伴可以參考下2016-09-09Python爬蟲基礎(chǔ)之爬蟲的分類知識總結(jié)
來給大家講python爬蟲的基礎(chǔ)啦,首先我們從爬蟲的分類開始講起,下文有非常詳細的知識總結(jié),對正在學習python的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05pycharm 實現(xiàn)本地寫代碼,服務(wù)器運行的操作
這篇文章主要介紹了pycharm 實現(xiàn)本地寫代碼,服務(wù)器運行的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06