简体   繁体   English

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

[英]How to schedule a Python script to run at a certain time?

Hello I am very new to programming and i'm trying to make an auto-posting bot for my subreddit.I'm using praw and I need to run this script at certain times and have it input and work 您好,我对编程非常陌生,我正在尝试为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")

I'm running windows and someone said to use the task scheduler but I haven't been able to figure it out. 我正在运行Windows,有人说要使用任务计划程序,但我无法弄清楚。 Any help would be great. 任何帮助都会很棒。 Thank You. 谢谢。

I would suggest to look into sched, a general purpose event scheduler. 我建议研究通用事件调度程序sched。 It is described, with appropriate examples to start you, in the Python's documentation . Python的文档中对此进行了描述,并提供了适合您的示例。

Sample: 样品:

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()

Change 3600 with the desired time in seconds. 将3600更改为所需的时间(以秒为单位)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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