溫馨提示×

溫馨提示×

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

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

使用Python操作微信的示例分析

發(fā)布時間:2021-12-14 17:37:40 來源:億速云 閱讀:172 作者:小新 欄目:大數(shù)據(jù)

這篇文章將為大家詳細(xì)講解有關(guān)使用Python操作微信的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。


 僅供學(xué)習(xí)參考使用

獲取好友列表

WechatPCAPI 提供了方法 get_friends(),該方法返回信息包括:好友、群和公眾號的列表信息,信息內(nèi)容主要包括:微信號、昵稱和自己設(shè)置的備注。

我們使用獲取的昵稱做個簡單的詞云展示,代碼實現(xiàn)如下所示:

logging.basicConfig(level=logging.INFO)

def on_message(message):
 pass

def get_friends():
 # 初始化微信實例
 wx_inst = WechatPCAPI(on_message=on_message, log=logging)
 # 啟動微信
 wx_inst.start_wechat(block=True)
 # 等待登陸成功,此時需要人為掃碼登錄微信
 while not wx_inst.get_myself():
  time.sleep(5)
 print('登陸成功')
 nicknames = []
 # 排除的詞
 remove = ['還是', '不會', '一些', '所以', '果然',
     '起來', '東西', '為什么', '真的', '這么',
     '但是', '怎么', '還是', '時候', '一個',
     '什么', '自己', '一切', '樣子', '一樣',
     '沒有', '不是', '一種', '這個', '為了'
   ]
 for key, value in wx_inst.get_friends().items():
  if key in ['fmessage', 'floatbottle', 'filehelper'] or 'chatroom' in key:
   continue
  nicknames.append(value['wx_nickname'])
 words = []
 for text in nicknames:
  if not text:
   continue
  for t in jieba.cut(text):
   if t in remove:
    continue
   words.append(t)
 global word_cloud
 # 用逗號隔開詞語
 word_cloud = ','.join(words)

def nk_cloud():
    # 打開詞云背景圖
    cloud_mask = np.array(Image.open('bg.png'))
    # 定義詞云的一些屬性
    wc = WordCloud(
        # 背景圖分割顏色為白色
        background_color='white',
        # 背景圖樣
        mask=cloud_mask,
        # 顯示最大詞數(shù)
        max_words=300,
        # 顯示中文
        font_path='./fonts/simkai.ttf',
        # 最大尺寸
        max_font_size=70
    )
    global word_cloud
    # 詞云函數(shù)
    x = wc.generate(word_cloud)
    # 生成詞云圖片
    image = x.to_image()
    # 展示詞云圖片
    image.show()
    # 保存詞云圖片
    wc.to_file('nk.png')
 

看一下效果:

使用Python操作微信的示例分析

 

消息防撤回

我們在使用微信和好友聊天時,對方有時會有撤回消息的情況,正常情況下,我們是不知道好友撤回的消息是什么的,通過 WechatPCAPI 就可以實現(xiàn)消息防撤回的功能。

我們知道通常撤回的消息是點擊撤回操作前一步發(fā)送的內(nèi)容,當(dāng)然也可能撤回的是前兩步、三步 ... 的消息,這里我們只對撤回前一步的消息做處理,基本思路是:我們將撤回前一步發(fā)送的消息存一下,當(dāng)對方點擊撤回操作時,我們再將前一步的消息再次返回給自己。

下面看一下實現(xiàn)代碼:

logging.basicConfig(level=logging.INFO)
queue_recved_event = Queue()

def on_message(msg):
    queue_recved_event.put(msg)

def login():
    pre_msg = ''
    # 初始化微信實例
    wx_inst = WechatPCAPI(on_message=on_message, log=logging)
    # 啟動微信
    wx_inst.start_wechat(block=True)
    # 等待登陸成功,此時需要人為掃碼登錄微信
    while not wx_inst.get_myself():
        time.sleep(5)
    print('登陸成功')
    while True:
        msg = queue_recved_event.get()
        data = msg.get('data')
        sendinfo = data.get('sendinfo')
        data_type = str(data.get('data_type'))
        msgcontent = str(data.get('msgcontent'))
        is_recv = data.get('is_recv')
        print(msg)
        if data_type == '1' and 'revokemsg' not in msgcontent:
            pre_msg = msgcontent
        if sendinfo is not None and 'revokemsg' in msgcontent:
            user = str(sendinfo.get('wx_id_search'))
            recall = '撤回的消息:' + pre_msg
            wx_inst.send_text(to_user=user, msg=recall)
 

看一下操作效果:

使用Python操作微信的示例分析

關(guān)于“使用Python操作微信的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

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

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

AI