如何安排Python脚本在特定时间运行?

2024-05-23 18:30:05 发布

您现在位置:Python中文网/ 问答频道 /正文

你好,我对编程很陌生,我正在尝试为我的subreddit制作一个自动发布机器人。我正在使用praw,我需要在某些时候运行这个脚本,并让它输入和工作

import praw

r = praw.Reddit(user_agent="UA")
r.login("username", "password")
sub = r.get_subreddit("Sub")
sub.submit("Title", text="Post text")

我正在运行windows,有人说要使用任务调度程序,但我还没有弄清楚。任何帮助都很好。谢谢您。


Tags: textimport脚本编程username机器人loginpassword
1条回答
网友
1楼 · 发布于 2024-05-23 18:30:05

我建议研究sched,一个通用的事件调度程序。本文将在Python's documentation中用适当的示例来描述它。

样品:

import time
import sched

scheduler = sched.scheduler(time.time, time.sleep)

def reddit():
  <your code>

def scheduler_reddit():

  scheduler.enter(0, 1, reddit, ())
  scheduler.run()
  time.sleep(3600)

for i in range(100):
  scheduler_reddit()

用所需时间(秒)更改3600。

相关问题 更多 >