如何使用python每隔60分钟重新编写一个脚本

如何使用python每隔60分钟重新编写一个脚本,python,selenium,Python,Selenium,我使用下面的脚本来自动化网站上的任务 如何使脚本每60分钟运行一次 我使用这段代码来完成我的任务,当我手动运行这段代码时,这段代码可以正常工作,但是我想运行这个脚本一次,然后每隔60分钟自动重复一次 这是我使用的代码 from selenium import webdriver from selenium.webdriver.common.keys import Keys import time PATH = "/usr/bin/chromedriver" driver = webdrive

我使用下面的脚本来自动化网站上的任务

如何使脚本每60分钟运行一次

我使用这段代码来完成我的任务,当我手动运行这段代码时,这段代码可以正常工作,但是我想运行这个脚本一次,然后每隔60分钟自动重复一次

这是我使用的代码

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

PATH = "/usr/bin/chromedriver"

driver = webdriver.Chrome(PATH)

driver.get("https://freebitco.in")
driver.maximize_window()
time.sleep(2)
driver.find_element_by_link_text('LOGIN').click()
time.sleep(3)
driver.find_element_by_id("login_form_btc_address").send_keys("EMAILADDRESS")
driver.find_element_by_id("login_form_password").send_keys("PASSWORD")
driver.find_element_by_id("login_button").click()
time.sleep(4)
driver.find_element_by_class_name("pushpad_deny_button").click()
time.sleep(3)
driver.find_element_by_id("free_play_form_button").click()
time.sleep(5)
driver.find_element_by_class_name("close-reveal-modal").click()
driver.quit()

我想每60分钟重复一次此脚本在代码中,您可以通过长时间睡眠来实现:

while True:
  # existing code goes here...
  time.sleep(60 * 60) # secs x mins

如果在某些情况下需要测试并停止,您可能需要将
while
条件更改为其他条件。

在代码中,您可以通过长时间睡眠来实现这一点:

while True:
  # existing code goes here...
  time.sleep(60 * 60) # secs x mins

如果在某些情况下需要测试并停止,您可能需要将
while
条件更改为其他条件。

在Python中可以像这样使用线程睡眠

import time
while True:
   #Your Code
   time.sleep(60)

在Python中可以像这样使用线程睡眠

import time
while True:
   #Your Code
   time.sleep(60)

首先将所有代码放入函数中。然后使用以下线程模块的定时器功能,在一定的时间间隔后重复该功能

import threading

def your_function():
  #your code inside function goes here. Make sure the below line is at last.
  threading.Timer(5.0, your_function).start()


your_function

# continue with the rest of your code

用所需时间替换
5.0

首先将所有代码放入函数中。然后使用以下线程模块的定时器功能,在一定的时间间隔后重复该功能

import threading

def your_function():
  #your code inside function goes here. Make sure the below line is at last.
  threading.Timer(5.0, your_function).start()


your_function

# continue with the rest of your code

用您想要的时间替换
5.0

这是否回答了您的问题?您是否考虑过将其作为cron作业运行?如果没有,这可能有助于重复或在某些时候运行像这样的一次性作业,这通常是cron或systemd的工作,但由于需要图形上下文,在这里使用它们可能会很麻烦。True@FallenWarrior,如果有问题,可能可以无头运行Selenium。另外,尝试一下这是否回答了您的问题?您是否考虑过将其作为cron作业运行?如果没有,这可能有助于重复或在某些时候运行像这样的一次性作业,这通常是cron或systemd的工作,但由于需要图形上下文,在这里使用它们可能会很麻烦。True@FallenWarrior,如果有问题,可能可以无头运行Selenium。还可以尝试这里的代码是什么?从路径到驱动程序退出,还是我也需要将导入内容放入这个循环中?我可以不写60*60,而是使用3600这样的秒数吗?您只需要
驱动程序.get()
和更高版本的代码-开始时的“设置”代码(
导入
等)只需要发生一次。您可以使用3600-我刚才以这种方式演示了它,以防您以后想创建一个“minutes”变量?从路径到驱动程序退出,还是我也需要将导入内容放入这个循环中?我可以不写60*60,而是使用3600这样的秒数吗?您只需要
驱动程序.get()
和更高版本的代码-开始时的“设置”代码(
导入
等)只需要发生一次。您可以使用3600—我刚才以这种方式演示了它,以防您以后要生成“minutes”变量。