Scheduled program doesn't run every time–why not?

Question:

I currently have a python program that is both a web-scraper, and file-writer which updates databases that are on my desktop using windows 10 task scheduler. The problem is, for some reason the task scheduler doesn’t run the python files at the specified time 100% of the time. I was wondering if there was a better approach to assure that the files get updated at their specified times, as long as the computer is on.

I’ve Tried changing the task scheduler settings, but I still have this problem.

import requests
from bs4 import BeautifulSoup
from datetime import datetime
#Updates Everyday.
#Fantasy5-WebScraper
response = requests.get('https://www.lotteryusa.com/michigan/fantasy-5/')
soup = BeautifulSoup(response.text, 'html.parser')
date = soup.find(class_='date')
results = soup.find(class_='draw-result list-unstyled list-inline')
d = datetime.strptime(date.time['datetime'], '%Y-%m-%d')
Fantasy5 = (d.strftime("%Y-%m-%d")+(',')+results.get_text().strip().replace('n',','))
print(Fantasy5)

#Writing to DataBase
with open("Filename.txt", "r") as f:
data = f.read()

with open("Filename.txt", "w") as f:
    f.write('{}{}{}'.format(Fantasy5, 'n' if data else '', data))
    f.close()

#Writing to DataFrame
with open("Filename.txt", "r") as f:
    data = f.read()

with open("Filename.txt", "w") as f:
    f.write('{}{}{}'.format(Fantasy5, 'n' if data else '', data))
    f.close()
Asked By: Jordan Graham

||

Answers:

You can use schedule to do this task. then add the python file to startup so it gets executed every time you start the computer.

this program will do the job every day at 6 am.

import schedule
import time
import requests
from bs4 import BeautifulSoup
from datetime import datetime

def job(t):
    response = requests.get('https://www.lotteryusa.com/michigan/fantasy-5/')
    soup = BeautifulSoup(response.text, 'html.parser')
    date = soup.find(class_='date')
    results = soup.find(class_='draw-result list-unstyled list-inline')
    d = datetime.strptime(date.time['datetime'], '%Y-%m-%d')
    Fantasy5 = (d.strftime("%Y-%m-%d")+(',')+results.get_text().strip().replace('n',','))
    print(Fantasy5)

    #Writing to DataBase
    with open("Filename.txt", "r") as f:
        data = f.read()

    with open("Filename.txt", "w") as f:
        f.write('{}{}{}'.format(Fantasy5, 'n' if data else '', data))
        f.close()

    #Writing to DataFrame
    with open("Filename.txt", "r") as f:
        data = f.read()

    with open("Filename.txt", "w") as f:
        f.write('{}{}{}'.format(Fantasy5, 'n' if data else '', data))
        f.close()
    return

schedule.every().day.at("06:00").do(job,'It is 06:00')

while True:
    schedule.run_pending()
    time.sleep(60)