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

时间:2019-07-05 01:29:34

标签: python web-scraping taskscheduler

我目前有一个既是网络抓取工具又是文件编写器的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()

1 个答案:

答案 0 :(得分:0)

您可以使用计划执行此任务。然后将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)