Python 想知道是否有更好的方法更新文件?

Python 想知道是否有更好的方法更新文件?,python,web-scraping,taskscheduler,Python,Web Scraping,Taskscheduler,我目前有一个python程序,它既是一个web刮板,又是一个文件编写器,可以使用Windows10任务调度器更新桌面上的数据库。问题是,出于某种原因,任务调度器没有在指定的时间100%运行python文件。我想知道是否有更好的方法来确保文件在指定的时间得到更新,只要计算机还在运行 我已尝试更改任务计划程序设置,但仍然存在此问题 import requests from bs4 import BeautifulSoup from datetime import datetime #Updates

我目前有一个python程序,它既是一个web刮板,又是一个文件编写器,可以使用Windows10任务调度器更新桌面上的数据库。问题是,出于某种原因,任务调度器没有在指定的时间100%运行python文件。我想知道是否有更好的方法来确保文件在指定的时间得到更新,只要计算机还在运行

我已尝试更改任务计划程序设置,但仍然存在此问题

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

您可以使用schedule来执行此任务。然后将python文件添加到startup,这样每次启动计算机时都会执行该文件

这个程序将在每天早上6点完成这项工作

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)