繁体   English   中英

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

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

我目前有一个 python 程序,它既是一个网络爬虫,又是一个文件编写器,它使用 Windows 10 任务调度程序更新我桌面上的数据库。 问题是,由于某种原因,任务调度程序不会在指定时间 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文件添加到启动中,以便每次启动计算机时都会执行它。

这个程序将在每天早上 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