Python 实现定时任务的八种方案,定时发微信

import datetime
import time

def time_printer():
    now = datetime.datetime.now()
    ts = now.strftime("%Y-%m-%d %H:%M:%S")
    print('do func time: ', ts)
    
def loop_monitor():
    while True:
        time_printer()
        time.sleep(5)
        
if __name__ == '__main__':
    loop_monitor()

调度模块schedule实现定时任务

import schedule
import time
def job():
    print("I'm working...")
schedule.every(10).seconds.do(job)
schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every(5).to(10).minutes.do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("13:15").do(job)
schedule.every().minute.at(":17").do(job)
while True:
    schedule.run_pending()
    time.sleep(1)


# 装饰器:通过 @repeat() 装饰静态方法
import time
from schedule import every, repeat, run_pending
@repeat(every().second)
def job():
    print('working...')
while True:
    run_pending()
    time.sleep(1)

万字总结!Python 实现定时任务的八种方案

定时发微信

import schedule
from datetime import datetime, timedelta, time
import subprocess
import uiautomation as auto
import time

   
 
   
def read_txt():
    txt_tables = []
    with open("./1.txt","r",encoding="utf-8") as f:
        line = f.readline() # 读取第一行
        while line:
            txt_tables.append(line.split("\n")[0])
            line = f.readline()
        return txt_tables
    print(txt_tables)
    
list1 = read_txt()
            
def send():   
    subprocess.Popen('C:\Program Files (x86)\Tencent\WeChat\WeChat.exe')
    search_object = '文件传输助手'
    wechatWindow = auto.WindowControl(Name="微信", ClassName='WeChatMainWndForPC')
    # 搜索
    search = wechatWindow.EditControl(Name='搜索')
    search.Click()
    search.SendKeys(search_object)
    # 给足够的时间
    time.sleep(2)
    wechatWindow.SendKeys('{Enter}')
    # 输入框对象
    edit = wechatWindow.EditControl(Name='输入')
    emoji_face = list1[0]
    a = list1.pop(0)
    if a is not None:
        edit.SendKeys(emoji_face)
        # 回车发送消息
        wechatWindow.SendKeys('{Enter}')
        print(emoji_face)





def job():
    send()
    


# schedule的用法
# schedule.every(10).seconds.do(job)
# schedule.every(10).minutes.do(job)
# schedule.every().hour.do(job)
# schedule.every().day.at("10:30").do(job)
# schedule.every(5).to(10).minutes.do(job)
schedule.every().monday.at("04:00").do(job)
# schedule.every().wednesday.at("13:15").do(job)
schedule.every().minute.at(":47").do(job)
schedule.every().second.until(timedelta(hours=1)).do(job)


# 也可以用装饰器的形式实现
# from schedule import every, repeat, run_pending
# @repeat(every().second)
# def job():
#     print('working...')



# 取消任务
# if i == 10:
#         schedule.cancel_job(job)



# 运行任务到某时间:
# schedule.every().second.until('23:59').do(job)  # 今天23:59停止
# schedule.every().second.until('2030-01-01 18:30').do(job)  # 2030-01-01 18:30停止
# schedule.every().second.until(timedelta(hours=8)).do(job)  # 8小时后停止
# schedule.every().second.until(time(23, 59, 59)).do(job)  # 今天23:59:59停止
# schedule.every().second.until(datetime(2030, 1, 1, 18, 30, 0)).do(job)  # 2030-01-01 18:30停止


while True:
    schedule.run_pending()
    
    




    

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值