Cronjob 不执行 python 脚本

Cronjob doesn't execute python script

我想使用 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 文件。

也许有人可以帮助我 :)

首先你必须确保你的脚本 运行s.

如果您 运行 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 的执行权限。