使用 Python 调度程序和 wget 定期下载文件
Regular file download with Python scheduler and wget
我写了一个简单的脚本,它使用 schedule
模块安排每周一次从网页下载文件。下载前,它会检查文件是否已使用 BeautifulSoup
更新。如果是,它将使用 wget
下载文件。此外,其他脚本使用该文件执行计算。
问题是文件不会出现在目录中,直到我手动中断脚本。所以,每次我都必须中断脚本并重新运行它,所以它会安排在下周。
是否有机会在不中断脚本的情况下“即时”下载和保存文件?
代码为:
import wget
import ssl
import schedule
import time
from bs4 import BeautifulSoup
import datefinder
from datetime import datetime
# disable certificate checks
ssl._create_default_https_context = ssl._create_unverified_context
#checking if file was updated, if yes, download file, if not waiting until updated
def download_file():
if check_for_updates():
print("downloading")
url = 'https://fgisonline.ams.usda.gov/ExportGrainReport/CY2020.csv'
wget.download(url)
print("downloading complete")
else:
print("sleeping")
time.sleep(60)
download_file()
# Checking if website was updated
def check_for_updates():
url2 = 'https://fgisonline.ams.usda.gov/ExportGrainReport/default.aspx'
html = urlopen(url2).read()
soup = BeautifulSoup(html, "lxml")
text_to_search = soup.body.ul.li.string
matches = list(datefinder.find_dates(text_to_search[30:]))
found_date = matches[0].date()
today = datetime.today().date()
return found_date == today
schedule.every().tuesday.at('09:44').do(download_file)
while True:
schedule.run_pending()
time.sleep(1)
根据以下线索,您应该能够解决您的问题:
from bs4 import BeautifulSoup
import requests
import urllib3
urllib3.disable_warnings()
def main(url):
r = requests.head(url, verify=False)
print(r.headers['Last-Modified'])
main("https://fgisonline.ams.usda.gov/ExportGrainReport/CY2020.csv")
输出:
Mon, 28 Sep 2020 15:02:22 GMT
现在您可以 运行 您的脚本通过 Cron
工作每天在您喜欢的时间循环遍历文件 headers
Last-Modified
直到它等于今天的日期,然后下载文件。
请注意,我使用了 head
请求,跟踪速度将提高 100 倍。然后你可以使用 requests.get
I prefer to work under the same session as well
您需要指定输出目录。我认为除非这样做,否则 PyCharm 会保存在某个临时目录中,当您停止脚本时 PyCharm 复制它。
更改为:
wget.download(url, out=output_directory)
我写了一个简单的脚本,它使用 schedule
模块安排每周一次从网页下载文件。下载前,它会检查文件是否已使用 BeautifulSoup
更新。如果是,它将使用 wget
下载文件。此外,其他脚本使用该文件执行计算。
问题是文件不会出现在目录中,直到我手动中断脚本。所以,每次我都必须中断脚本并重新运行它,所以它会安排在下周。
是否有机会在不中断脚本的情况下“即时”下载和保存文件?
代码为:
import wget
import ssl
import schedule
import time
from bs4 import BeautifulSoup
import datefinder
from datetime import datetime
# disable certificate checks
ssl._create_default_https_context = ssl._create_unverified_context
#checking if file was updated, if yes, download file, if not waiting until updated
def download_file():
if check_for_updates():
print("downloading")
url = 'https://fgisonline.ams.usda.gov/ExportGrainReport/CY2020.csv'
wget.download(url)
print("downloading complete")
else:
print("sleeping")
time.sleep(60)
download_file()
# Checking if website was updated
def check_for_updates():
url2 = 'https://fgisonline.ams.usda.gov/ExportGrainReport/default.aspx'
html = urlopen(url2).read()
soup = BeautifulSoup(html, "lxml")
text_to_search = soup.body.ul.li.string
matches = list(datefinder.find_dates(text_to_search[30:]))
found_date = matches[0].date()
today = datetime.today().date()
return found_date == today
schedule.every().tuesday.at('09:44').do(download_file)
while True:
schedule.run_pending()
time.sleep(1)
根据以下线索,您应该能够解决您的问题:
from bs4 import BeautifulSoup
import requests
import urllib3
urllib3.disable_warnings()
def main(url):
r = requests.head(url, verify=False)
print(r.headers['Last-Modified'])
main("https://fgisonline.ams.usda.gov/ExportGrainReport/CY2020.csv")
输出:
Mon, 28 Sep 2020 15:02:22 GMT
现在您可以 运行 您的脚本通过 Cron
工作每天在您喜欢的时间循环遍历文件 headers
Last-Modified
直到它等于今天的日期,然后下载文件。
请注意,我使用了 head
请求,跟踪速度将提高 100 倍。然后你可以使用 requests.get
I prefer to work under the same session as well
您需要指定输出目录。我认为除非这样做,否则 PyCharm 会保存在某个临时目录中,当您停止脚本时 PyCharm 复制它。
更改为:
wget.download(url, out=output_directory)