我试图在每天的某个时间执行我的代码,但它不起作用

I am trying to execute my code at a certain time every day and its not working

我是不是做错了什么。我一直在努力解决这个问题。它打印一次然后保留 运行 但不打印任何其他内容。

import schedule
import time

def bot():
    schedule.every(1).day.at("6:41").do(bot)
    print("ya")

while True:
    schedule.run_pending()
    time.sleep(1)

查看文档 here 以了解它应该如何工作的一个很好的例子

简单的答案是你的 bot() 试图递归地执行自己,但是当它 运行 自己时发生的唯一事情是它告诉自己再次 运行 自己有一天 6:41...同时没有 print() 语句被执行,因为它不存在于 bot() 函数中。

您的代码执行一次 print() 语句,因为它 运行 在定义 bot().

之后立即执行它

您永远不会在此代码中调用 bot(),因此该函数实际上不会执行任何操作。

简单的解决方案是将您想要的功能放入 bot() 函数中,然后将其添加到 schedule 中,如下所示:

import schedule
import time

def bot():
    print("ya")

schedule.every(1).day.at("6:41").do(bot)

while True:
    schedule.run_pending()
    time.sleep(1)