用python实现微信定时发送图片

用电脑微信实现定时给好友发送多张图片

一、需要安装的库:

apscheduler
os
pyautogui
pyperclip
time
PIL
win32clipboard,win32con

二、定义几个函数

(1)

def send():#发送图片的函数
    # 复制需要发送的内容到粘贴板
    # 模拟键盘 ctrl + v 粘贴内容
    pyautogui.hotkey('ctrl', 'v')
    # 发送消息
    pyautogui.press('enter')

(2)

def serch_friend(friend):
    # Ctrl + alt + w 打开微信
    pyautogui.hotkey('ctrl', 'alt', 'w')
    # 搜索好友
    pyautogui.hotkey('ctrl', 'f')
    # 复制好友昵称到粘贴板
    pyperclip.copy(friend)
    # 模拟键盘 ctrl + v 粘贴
    pyautogui.hotkey('ctrl', 'v')
    time.sleep(1)
    # 回车进入好友消息界面
    pyautogui.press('enter')
    get_img()

(3)

def get_img():
    imagepath = "D:/xunlei/pic"  # 自己的图片文件夹路径
    all = os.walk(imagepath)
    i = 0
    for path, dir, file_list in all:  # 遍历图片
        print(path, dir, file_list)

        for filename in file_list:
            if filename.endswith('.jpg') or filename.endswith('.png'):
                clip.OpenClipboard()  # 打开剪贴板
                clip.EmptyClipboard()  # 先清空剪贴板
                image = os.path.join(imagepath, path, filename)
                print(image)

                img = Image.open(image)
                output = BytesIO()
                img.convert("RGB").save(output, "BMP")
                data = output.getvalue()[14:]
                output.close()
                clip.SetClipboardData(win32con.CF_DIB, data)  # 将图片放入剪贴板
                clip.CloseClipboard()
                time.sleep(1)
                send()
                i += 1
                if i > 5:  # 想发送 n 张就设置 i > n-1(3 为发送的最大数量-1 i从0开始 所以设置为3,是4张图片:  0 1 2 3)
                    print('发送完成')
                    return 0

(4)


```python
def send_message():
    friend_name = "文件传输助手"#要发送的好友名字

    serch_friend(friend_name)

三、进行定时设计

#利用apscheduler模块实现定时发送,运行send_message主函数
if __name__ == '__main__':

    # 设置时域
    scheduler = BlockingScheduler(timezone="Asia/Shanghai")
    # 时间设置,每年的10月,每天的8-11,2-4点半,
    scheduler.add_job(send_message, 'cron', month='10', hour='8-11,2-4', minute='30')
    # 定时语言
    try:
        scheduler.start()
    except (KeyboardInterrupt, SystemExit):
        pass

参考的文献:[微信发送图片](https://blog.csdn.net/q1246192888/article/details/123954908?ops_request_misc=&request_id=&biz_id=102&utm_term=%E5%BE%AE%E4%BF%A1%E5%AE%9A%E6%97%B6%E4%BC%A0%E9%80%81%E5%9B%BE%E7%89%87&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-0-123954908.142%5Ev56%5Epc_rank_34_queryrelevant25,201%5Ev3%5Econtrol_2&spm=1018.2226.3001.4187)
  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要现微信定时发送功能,你可以使用Python的itchat库来控制微信的登录和消息发送。以下是一个简单的示例代码: ```python import itchat import time def send_message(): # 登录微信 itchat.auto_login() # 获取好友列表,可以根据需要选择发送消息的好友 friends = itchat.get_friends(update=True) # 遍历好友列表,发送消息 for friend in friends: # 过滤掉特定的好友,可以根据需要修改判断条件 if friend['RemarkName'] == '某某好友': # 发送消息 itchat.send('定时发送的消息', friend['UserName']) # 延时1秒,避免发送频率过高 time.sleep(1) # 退出登录 itchat.logout() # 设置定时发送的时间,这里设置为每天的9点30分 schedule_time = '09:30' while True: # 获取当前时间 current_time = time.strftime('%H:%M', time.localtime()) # 判断是否到达设定的时间 if current_time == schedule_time: # 发送消息 send_message() # 延时一天,避免重复发送 time.sleep(24 * 60 * 60) else: # 延时1秒后再次检查时间 time.sleep(1) ``` 上述代码使用itchat库实现微信登录和消息发送功能。首先调用`itchat.auto_login()`登录微信,然后使用`itchat.get_friends()`获取好友列表。遍历好友列表,根据需要判断发送消息的对象,并使用`itchat.send()`发送消息。最后调用`itchat.logout()`退出登录。 代码中使用了一个无限循环,每次循环判断当前时间是否等于设定的定时发送时间。如果到达设定时间,则调用`send_message()`函数发送消息,并延时一天以避免重复发送。如果未到达设定时间,则延时一秒后再次检查时间。 请注意,定时发送功能需要保持脚本的运行,可以在服务器上运行或者使用类似于crontab的定时任务工具来执行脚本。此外,使用itchat库进行微信登录可能存在一定的风险,请谨慎使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值