簡體   English   中英

如何使用 python 每 60 分鍾重復一次腳本

[英]How can I repat a script every 60 minutes with python

我使用以下腳本自動執行網站上的任務

我怎樣才能使這個腳本每 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條件更改為其他條件。

您可以在 Python 中使用這樣的線程睡眠。

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

首先將所有代碼放入 function 中。 然后使用如下線程模塊的定時器 function 在一定的時間間隔后重復 function。

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替換為您想要的時間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM