Skip to content Skip to sidebar Skip to footer

How To Run A Function Everyday At A Certain Time Then Stop After A Specific Hour?

I have a script that will scrape Facebook post in a while loop. But to prevent getting banned by Facebook I prefer the scraping function run(self) only run at a certain time and st

Solution 1:

Thanks to the respond from @larsks, I tried to put a time check inside the function and exit the loop if it reached 3600second which is equal to 1 hour.

Here is the sample code:

import schedule
import time


def job():
    program_starts = time.time()

    while True:
        now = time.time()
        timeframe = now - program_starts
        print("It has been {0} seconds since the loop started".format(timeframe))
        if timeframe > 3600:
            break

schedule.every().day.at("19:20:30").do(job)

while True:
    schedule.run_pending()
    #time.sleep(60) # wait one minute

Post a Comment for "How To Run A Function Everyday At A Certain Time Then Stop After A Specific Hour?"