簡體   English   中英

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 文件。

也許有人可以幫助我:)

首先,您必須確保您的腳本運行。

如果您以交互方式運行python3 auslastung.py為什么在您的 cron 上以不同的方式調用您的 python 腳本。

您是否嘗試/home/pi/Desktop/repository/auslastung_download/auslastung.py交互方式運行/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執行chmod +x auslastung.py

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM