简体   繁体   English

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

[英]Wondering if there is a better way to update files?

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.我目前有一个 python 程序,它既是一个网络爬虫,又是一个文件编写器,它使用 Windows 10 任务调度程序更新我桌面上的数据库。 The problem is, for some reason the task scheduler doesn't run the python files at the specified time 100% of the time.问题是,由于某种原因,任务调度程序不会在指定时间 100% 的时间运行 python 文件。 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()

You can use schedule to do this task.您可以使用 schedule 来完成此任务。 then add the python file to startup so it gets executed every time you start the computer.然后将python文件添加到启动中,以便每次启动计算机时都会执行它。

this program will do the job every day at 6 am.这个程序将在每天早上 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM