【发布时间】:2021-01-22 12:20:39
【问题描述】:
我想在一天中的每个小时都使用 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.pyeverything 执行时,一切正常,脚本写入 data.csv 文件。
也许有人可以帮助我:)
【问题讨论】:
-
脚本顶部没有
#!/usr/bin/python,因此您需要在指向脚本路径之前调用python3 -
你错过了脚本第一行的 shebang ...类似于
#!/usr/bin/env python -
我添加了 #!/usr/bin/python bur,它仍然无法正常工作。我也对文件做了 chmod +x
-
尝试查找
/usr/bin/python是否是您的python 解释器。使用which python3找到它。 -
usr/bin/python是根据which python的解释器
标签: python linux cron raspberry-pi