我想使用Cron每天每小时执行一次python脚本。因此,我创建了一个cronjob,看起来像:@hourly /home/pi/Desktop/repository/auslastung_download/auslastung.py
cronjob应执行以下脚本:
from bs4 import BeautifulSoup
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium import webdriver
from datetime import datetime, date
def get_auslastung_lichtenberg():
try:
url = "https://www.mcfit.com/de/fitnessstudios/studiosuche/studiodetails/studio/berlin-lichtenberg/"
options = FirefoxOptions()
options.add_argument("--headless")
driver = webdriver.Firefox(options=options)
driver.get(url)
html_content = driver.page_source
soup = BeautifulSoup(html_content, 'html.parser')
elems = soup.find_all('div', {'class': 'sc-iJCRLp eDJvQP'})
#print(elems)
auslastung = str(elems).split("<span>")[1]
#print(auslastung)
auslastung = auslastung[:auslastung.rfind('</span>')]
#print(auslastung)
auslastung = str(auslastung).split("Auslastung ")[1]
#print(auslastung)
auslastung = auslastung[:auslastung.rfind('%')]
print(auslastung)
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
#print("Current Time =", current_time)
today = date.today()
print(today)
ergebnis = {'date': today, 'time': current_time,'studio': "Berlin Lichtenberg", 'auslastung': auslastung}
return ergebnis
finally:
try:
driver.close()
except:
pass
"""
import json
with open('database.json', 'w') as f:
json.dump(get_auslastung_lichtenberg(), f)
"""
import csv
with open('/home/pi/Desktop/repository/auslastung_download/data.csv', mode='a') as file:
fieldnames = ['date', 'time', 'studio', 'auslastung']
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writerow(get_auslastung_lichtenberg())
通过python3 auslastung.py
执行时,一切正常,脚本写入data.csv文件
也许有人能帮我:)
首先,必须确保脚本运行正常
如果以交互方式运行
python3 auslastung.py
,为什么在cron上以不同的方式调用python脚本您是否尝试过以交互方式运行
/home/pi/Desktop/repository/auslastung_download/auslastung.py
?如果没有初始python3
,它会运行吗如果脚本在crontab上使用
python3 auslastung.py
运行,则应包括解释器和脚本的完整路径:@hourly /paht/to/python3 /full/path/to/script.py
如果您让脚本直接运行而不需要指示解释器,只需
/full/path/to/script.py
,那么在crontab上您应该包含脚本的完整路径:@hourly /full/path/to/script.py
您可以包括一个shebang:脚本的第一行指示使用哪个解释器来执行它。所以你的第一行应该是
#!/path/to/your/interpreter
您必须确保脚本具有
chmod +x auslastung.py
的执行权限相关问题 更多 >
编程相关推荐