简体   繁体   中英

How to run a function every day at a specific time in python?

So, here is what I've been trying to do. I'm making a little program that goes to my classes on time so I don't miss them. And I have problems with the most important part... The automation: This is my code:

from pyautogui import *
import pyautogui
import keyboard
from pynput.keyboard import Key, Controller
import webbrowser

pos = pyautogui.position()
print(pos)

def open_b():
    pyautogui.click(248, 754)
    url='https://myclass.com'
    webbrowser.open_new_tab(url)```

A straightforward solution would be:

import time
import datetime

# Enter the first class date and time
class_time = datetime.datetime(year, month, day, hour=13, minute=30, second=0)

delta = datetime.timedelta(days=1)
while True:
    
    if datetime.datetime.now() >= class_time:
        your_function()
        class_time += delta

    # Depending on need, check frequency can be decreased
    time.sleep(1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM