栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python实现微信自动发送消息

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python实现微信自动发送消息

目标:

①Python定时发送微信消息;

②Python实时发送微信消息。

使用键位编码win32api、win32con
或者send_keys实现发送微信消息。

首先以win32api、win32con为例:

"""
实现定时自动发送消息
"""

import time
import pyperclip
import win32api
import win32con
import os

while True:
    time_now = time.strftime("%H:%M:%S", time.localtime())  # 获取当前时间
    sent_time = '16:57:34'  # 发送时间
    if time_now == sent_time:  # 当前时间等于发送时间则执行以下程序
        def open_app(app_dir):
            os.startfile(app_dir)

        # 打开微信
        if __name__ == "__main__":
            app_dir = r'C:Program Files (x86)TencentWeChatWeChat.exe'  # 此处为微信的绝对路径
            open_app(app_dir)
            time.sleep(1)  
        
        #进入微信,模拟按键Ctrl+F
        win32api.keybd_event(17, 0, 0, 0)  # Ctrl
        win32api.keybd_event(70, 0, 0, 0)  # F
        win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(70, 0, win32con.KEYEVENTF_KEYUP, 0)
        time.sleep(1)

        # 复制需要查找的人,按回车,进入聊天输入框
        pyperclip.copy('请输入你需要聊天的联系人')  # 联系人昵称
        spam = pyperclip.paste()
        win32api.keybd_event(17, 0, 0, 0)  # Ctrl
        win32api.keybd_event(86, 0, 0, 0)  # 86→V;
        win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)
        time.sleep(1)
        win32api.keybd_event(13, 0, 0, 0)  # 13→Enter
        win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)
        time.sleep(1)

        #聊天输入框复制聊天内容,然后按回车发送消息
        pyperclip.copy('Hello~')  # 聊天的内容
        spam = pyperclip.paste()
        win32api.keybd_event(17, 0, 0, 0)  # Ctrl
        win32api.keybd_event(86, 0, 0, 0)  # 86→V
        win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(13, 0, 0, 0)
        win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)
        time.sleep(1)  # 确保程序只执行一次,防止重复执行

        pyperclip.copy('是时候点餐啦!!')  # 聊天的内容
        spam = pyperclip.paste()
        win32api.keybd_event(17, 0, 0, 0)  # Ctrl
        win32api.keybd_event(86, 0, 0, 0)  # 86→V
        win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(13, 0, 0, 0)
        win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)
        time.sleep(3)
        
        exit() # 退出程序




运行结果:

 实时发送消息:

"""
实时自动发送消息
"""


import time
import pyperclip
import win32api
import win32con
import os

i=0;
while i<1:
    i = i + 1;
    time_now = time.strftime("%H:%M", time.localtime())  # 获取当前时间
    sent_time = time.strftime("%H:%M", time.localtime())  # 发送时间
    if time_now == sent_time:  # 当前时间等于发送时间则执行以下程序
        def open_app(app_dir):
            os.startfile(app_dir)


        if __name__ == "__main__":
            app_dir = r'C:Program Files (x86)TencentWeChatWeChat.exe'  # 此处为微信的绝对路径
            open_app(app_dir)
            time.sleep(1)  # 电脑反应需要时间,使程序暂停一段时间来等待电脑反应,单位是秒

        win32api.keybd_event(17, 0, 0, 0)  # Ctrl
        win32api.keybd_event(70, 0, 0, 0)  # F
        win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(70, 0, win32con.KEYEVENTF_KEYUP, 0)
        time.sleep(1)

        pyperclip.copy('请输入联系人')  # 联系人
        spam = pyperclip.paste()
        win32api.keybd_event(17, 0, 0, 0)  # Ctrl
        win32api.keybd_event(86, 0, 0, 0)  # 86→V;  67→C
        win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)
        time.sleep(1)
        win32api.keybd_event(13, 0, 0, 0)  # 13→Enter
        win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)
        time.sleep(1)

        pyperclip.copy('Hello~')  # 聊天的内容
        spam = pyperclip.paste()
        win32api.keybd_event(17, 0, 0, 0)  # Ctrl
        win32api.keybd_event(86, 0, 0, 0)  # 86→V
        win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(13, 0, 0, 0)
        win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)
        time.sleep(1)  # 确保程序只执行一次,防止重复执行

        pyperclip.copy('是时候点餐啦!!')  # 聊天的内容
        spam = pyperclip.paste()
        win32api.keybd_event(17, 0, 0, 0)  # Ctrl
        win32api.keybd_event(86, 0, 0, 0)  # 86→V
        win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(13, 0, 0, 0)
        win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)
        time.sleep(5)  # 确保程序只执行一次,防止重复执行

else:
    print('停止运行!!')
    exit()




Python中模拟键盘输入键位编号对照表_baidu_17201253的博客-CSDN博客

发现另外一种更简单的方式,引用send_keys: 

"""
实现自动发送消息
"""

import time
import os
from pywinauto.keyboard import send_keys #键盘

while True:
    time_now = time.strftime("%H:%M:%S", time.localtime())  # 获取当前时间
    sent_time = time.strftime("%H:%M:%S", time.localtime())  # 发送时间
    if time_now == sent_time:  # 当前时间等于发送时间则执行以下程序
        def open_app(app_dir):
            os.startfile(app_dir)

        # 打开微信
        if __name__ == "__main__":
            app_dir = r'C:Program Files (x86)TencentWeChatWeChat.exe'  # 此处为微信的绝对路径
            open_app(app_dir)
            time.sleep(1)

        #进入微信,模拟按键Ctrl+F
        send_keys('^f')
        send_keys('请输入联系人')
        time.sleep(1)
        send_keys('{ENTER}') # 回车键必须全部大小

        #需要发送的消息内容
        message = '啦啦!!,是时候休息啦!Good night!!!'
        time.sleep(1)

        # 输入聊天内容
        send_keys(message)
        # 回车发送消息
        send_keys('{ENTER}')

        time.sleep(3)
        print('退出~~~')

        exit() # 退出程序




 仅用于学习,请勿用于刷屏!!!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/960379.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号