黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

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

系統(tǒng) 1975 0

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

itchat是python的一個(gè)api,可以訪問自己的微信信息,功能還蠻好玩的,可以扒取朋友信息,自動(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ù),比如說isGroupChat=True用來只自動(dòng)回復(fù)群聊信息


          

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

MAP 地理位置的分享
CARD 名片信息
SHARING 鏈接分享
PICTURE 表情或照片
RECORDING 語(yǔ)音
ATTACHMENT 附件
VIDEO 視頻
FRIENDS 加好友申請(qǐng),也就是說發(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,以空格隔開
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)查看自己微信的信息功能詳解_第1張圖片

更多關(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ì)有所幫助。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論