溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python使用微信itchat接口實(shí)現(xiàn)查看自己微信的信息功能詳解

發(fā)布時間:2020-09-09 04:20:11 來源:腳本之家 閱讀:1660 作者:thormas1996 欄目:開發(fā)技術(shù)

本文實(shí)例講述了Python使用微信itchat接口實(shí)現(xiàn)查看自己微信的信息功能。分享給大家供大家參考,具體如下:

itchat是python的一個api,可以訪問自己的微信信息,功能還蠻好玩的,可以扒取朋友信息,自動回復(fù)短信等等。

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其實(shí)是一個id,nickname是微信名字,remarkname是備注名

自動回復(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ù),比如說isGroupChat=True用來只自動回復(fù)群聊信息

register還可以注冊其他參數(shù):

MAP 地理位置的分享
CARD 名片信息
SHARING 鏈接分享
PICTURE 表情或照片
RECORDING 語音
ATTACHMENT 附件
VIDEO 視頻
FRIENDS 加好友申請,也就是說發(fā)起的一個好友申請其實(shí)是一條特殊的信息
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é)果:

 Python使用微信itchat接口實(shí)現(xiàn)查看自己微信的信息功能詳解

更多關(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文件與目錄操作技巧匯總》

希望本文所述對大家Python程序設(shè)計(jì)有所幫助。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI