Python使用微信itchat接口實(shí)現(xiàn)查看自己微信的信息功能詳解
本文實(shí)例講述了Python使用微信itchat接口實(shí)現(xiàn)查看自己微信的信息功能。分享給大家供大家參考,具體如下:
itchat是python的一個(gè)api,可以訪問(wèn)自己的微信信息,功能還蠻好玩的,可以扒取朋友信息,自動(dòng)回復(fù)短信等等。
package:
itchat1.3.10 + python3.5 + wordcloud1.4.1
登錄登出:
itchat.login() #hotReload設(shè)置為True,可以保持一段時(shí)間登錄 itchat.autologin(hotReload=True) itchat.logout()
獲取朋友數(shù)據(jù):
friends = itchat.get_friends(update=True)[0:]
搜索某個(gè)朋友:
itchat.search_friends(name='name') itchat.search_friends(wechatAccount='wechatid')
公眾號(hào)和群聊的獲取方式也是類似的:
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其實(shí)是一個(gè)id,nickname是微信名字,remarkname是備注名
自動(dòng)回復(fù)信息:
@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ù),比如說(shuō)isGroupChat=True用來(lái)只自動(dòng)回復(fù)群聊信息
register還可以注冊(cè)其他參數(shù):
| MAP | 地理位置的分享 |
| CARD | 名片信息 |
| SHARING | 鏈接分享 |
| PICTURE | 表情或照片 |
| RECORDING | 語(yǔ)音 |
| ATTACHMENT | 附件 |
| VIDEO | 視頻 |
| FRIENDS | 加好友申請(qǐng),也就是說(shuō)發(fā)起的一個(gè)好友申請(qǐng)其實(shí)是一條特殊的信息 |
| SYSTEM | 系統(tǒng)消息,比如系統(tǒng)推送消息或者是某個(gè)群成員發(fā)生變動(dòng)等等 |
| NOTE | 通知文本,比如撤回了消息等等 |
例子:拉取朋友數(shù)據(jù),用wordcloud可視化朋友signature
先讀取數(shù)據(jù)
import itchat itchat.login() friends = itchat.get_friends(update=True)[0:]
簡(jiǎn)單分析下性別比例
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))
獲得各個(gè)參數(shù),存入本地
filename = '' #需要修改這里
#爬取各個(gè)變量
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,以空格隔開(kāi)
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ù)學(xué)運(yùn)算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- python調(diào)用API接口實(shí)現(xiàn)登陸短信驗(yàn)證
- 微信域名檢測(cè)接口調(diào)用演示步驟(含PHP、Python)
- python itchat實(shí)現(xiàn)調(diào)用微信接口的第三方模塊方法
- Python 通過(guò)調(diào)用接口獲取公交信息的實(shí)例
- python實(shí)現(xiàn)微信接口(itchat)詳細(xì)介紹
- Python調(diào)用微信公眾平臺(tái)接口操作示例
- Python+微信接口實(shí)現(xiàn)運(yùn)維報(bào)警
- Python基于Twilio及騰訊云實(shí)現(xiàn)國(guó)際國(guó)內(nèi)短信接口
相關(guān)文章
Python數(shù)據(jù)分析之雙色球基于線性回歸算法預(yù)測(cè)下期中獎(jiǎng)結(jié)果示例
這篇文章主要介紹了Python數(shù)據(jù)分析之雙色球基于線性回歸算法預(yù)測(cè)下期中獎(jiǎng)結(jié)果,涉及Python基于線性回歸算法的數(shù)值運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2018-02-02
使用Python通過(guò)win32 COM打開(kāi)Excel并添加Sheet的方法
今天小編就為大家分享一篇使用Python通過(guò)win32 COM打開(kāi)Excel并添加Sheet的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Python+OpenCV實(shí)戰(zhàn)之利用?K-Means?聚類進(jìn)行色彩量化
這篇文章主要介紹了如何利用?K-Means?聚類進(jìn)行色彩量化,以減少圖像中顏色數(shù)量。文中的代碼具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以關(guān)注一下2021-12-12
在Pytorch中使用Mask R-CNN進(jìn)行實(shí)例分割操作
這篇文章主要介紹了在Pytorch中使用Mask R-CNN進(jìn)行實(shí)例分割操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
Python 實(shí)現(xiàn) 貪吃蛇大作戰(zhàn) 代碼分享
本文給大家分享的是一個(gè)使用cocos2d-python游戲引擎庫(kù)制作出來(lái)的貪吃蛇大作戰(zhàn)的游戲代碼,基于Python 2.7 和 cocos2d 庫(kù),有需要的小伙伴可以參考下2016-09-09
Python爬蟲(chóng)基礎(chǔ)之爬蟲(chóng)的分類知識(shí)總結(jié)
來(lái)給大家講python爬蟲(chóng)的基礎(chǔ)啦,首先我們從爬蟲(chóng)的分類開(kāi)始講起,下文有非常詳細(xì)的知識(shí)總結(jié),對(duì)正在學(xué)習(xí)python的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05
Python實(shí)現(xiàn)圣誕樹(shù)的多種方法
這篇文章主要為大家介紹了Python實(shí)現(xiàn)圣誕樹(shù)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2021-12-12
pycharm 實(shí)現(xiàn)本地寫代碼,服務(wù)器運(yùn)行的操作
這篇文章主要介紹了pycharm 實(shí)現(xiàn)本地寫代碼,服務(wù)器運(yùn)行的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06

