Python 为什么我的代码在使用crontab运行时只部分运行?

Python 为什么我的代码在使用crontab运行时只部分运行?,python,python-3.x,cron,pycharm,Python,Python 3.x,Cron,Pycharm,当我在PyCharm中运行python脚本时,整个脚本运行得非常完美。但是,当它从crontab运行时,它仅部分运行。特别是在“”下的代码末尾,检查播客是否已下载“”。我的crontab日志正在工作,没有输出错误 如果您想查看整个项目,请参阅github回购协议: 下面是crontab: */30****/usr/bin/python3/home/alex/PycharmProjects/YoutubeToAudioToPhone/main.py>~/cron.log 2>&1 这是我的代码:

当我在PyCharm中运行python脚本时,整个脚本运行得非常完美。但是,当它从crontab运行时,它仅部分运行。特别是在“”下的代码末尾,检查播客是否已下载“”。我的crontab日志正在工作,没有输出错误

如果您想查看整个项目,请参阅github回购协议:

下面是crontab:

*/30****/usr/bin/python3/home/alex/PycharmProjects/YoutubeToAudioToPhone/main.py>~/cron.log 2>&1

这是我的代码:

from selenium.webdriver.chrome.options import Options
from selenium import webdriver
import datefinder
from datetime import timedelta, date
import sys
from pytube import YouTube
from os import listdir, remove
from os.path import isfile, join

"""get paths"""
config = ConfigParser()
config.read("ENTER THE ABSOLUTE PATH TO YOUR my_paths.ini HERE")
chromeDriverPath = config.get("PATHS", "chromedriver")
previousPodPath = config.get("PATHS", "previousPod")
podcastOut = config.get("PATHS", "podcastOut")

"""get name of video and video link"""
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(executable_path=chromeDriverPath, options=chrome_options)
driver.get("https://www.youtube.com/c/Level1Techs/videos")
level1TechsURLTitle = driver.find_element_by_partial_link_text("Level1 News")
level1TechsURL = level1TechsURLTitle.get_attribute("href")

"""check video date, convert to string and split"""
level1TechsURLTitleString = str(level1TechsURLTitle.text)
split_string = level1TechsURLTitleString.split(":", 1)
substring = split_string[0]
matches = list(datefinder.find_dates(substring))
datePod = None
if len(matches) > 0:
    datePod = matches[0]
datePodString = str(datePod)
datePodStringSplt = datePodString.split(" ", 1)
datePodStringSpltSub = datePodStringSplt[0]

"""check if podcasts is already downloaded"""
previousPodsR = open(previousPodPath, "r")
lastPod = previousPodsR.readline()
if str(lastPod) == level1TechsURLTitleString:
    driver.quit()
    previousPodsR.close()
    sys.exit()
elif str(lastPod) != level1TechsURLTitleString:
    previousPodsR.close()
    previousPodsW = open(previousPodPath, "w")
    previousPodsW.write(level1TechsURLTitleString)
    previousPodsW.close()

"""get current date and convert to string"""
date = date.today()
dateString = str(date)

"""quit selenium"""
driver.quit()

"""download and name audio file"""
download = None
if dateString == datePodStringSpltSub:
    download = YouTube(str(level1TechsURL)).streams.get_audio_only().download(
        output_path=podcastOut, filename=level1TechsURLTitleString)

"""delete videos past certain date"""
daysBeforeDeletion = 3
onlyFiles = [f for f in listdir(podcastOut) if isfile(join(
    podcastOut, f))]
deletion_date = date.today() - timedelta(daysBeforeDeletion)
iterator = 0
for file in onlyFiles:
    fileDate = list(datefinder.find_dates(onlyFiles[iterator]))
    if not fileDate:
        break
    else:
        if fileDate[0].date() < deletion_date:
            remove(podcastOut + onlyFiles[iterator])
        iterator += 1

来自selenium.webdriver.chrome.options导入选项
从selenium导入webdriver
导入日期查找器
从日期时间导入时间增量,日期
导入系统
从pytube导入YouTube
从os import listdir中,删除
从os.path导入isfile,加入
“获取路径”
config=ConfigParser()
config.read(“在此处输入my_paths.ini的绝对路径”)
chromeDriverPath=config.get(“路径”,“chromedriver”)
previousPodPath=config.get(“路径”,“previousPod”)
podcastOut=config.get(“路径”、“podcastOut”)
“”“获取视频和视频链接的名称”“”
chrome_options=options()
chrome\u选项。添加\u参数(“--no sandbox”)
chrome_选项。添加_参数(“--disable dev shm usage”)
chrome_选项。添加_参数('--headless')
driver=webdriver.Chrome(可执行文件路径=chromeDriverPath,选项=Chrome\u选项)
驱动程序。获取(“https://www.youtube.com/c/Level1Techs/videos")
level1techsurltite=驱动程序。通过部分链接文本(“Level1新闻”)查找元素
level1TechsURL=level1techsurltite.get_属性(“href”)
“”“检查视频日期,转换为字符串并拆分”“”
level1TechsURLTitleString=str(level1TechsURLTitle.text)
split_string=level1TechsURLTitleString.split(“:”,1)
子字符串=拆分字符串[0]
匹配=列表(datefinder.find_dates(子字符串))
datePod=None
如果len(匹配)>0:
datePod=matches[0]
datePodString=str(datePod)
datePodStringSplt=datePodString.split(“,1)
datePodStringSpltSub=datePodStringSplt[0]
“”“检查播客是否已下载”“”
previousPodsR=打开(previousPodPath,“r”)
lastPod=previousPodsR.readline()
如果str(lastPod)=level1TechsURLTitleString:
driver.quit()
上一篇博客关闭()
sys.exit()
埃利夫街(最后一站)!=level1TechsURLTitleString:
上一篇博客关闭()
previousPodsW=打开(previousPodPath,“w”)
previousPodsW.write(level1TechsURLTitleString)
上一个podsw.close()文件
“”“获取当前日期并转换为字符串”“”
日期=日期。今天()
dateString=str(日期)
“退出硒”
driver.quit()
“”“下载并命名音频文件”“”
下载=无
如果dateString==datePodStringSpltSub:
download=YouTube(str(level1TechsURL)).streams.get_audio_only().download(
输出(路径=podcastOut,文件名=level1TechsURLTitleString)
“”“删除超过特定日期的视频”“”
日前删除=3
如果是isfile(join),则onlyFiles=[f代表listdir(podcastOut)中的f(
podcustout,f))]
删除日期=日期.今天()-timedelta(日期在删除之前)
迭代器=0
对于仅文件中的文件:
fileDate=list(datefinder.find_日期(仅限文件[迭代器])
如果不是文件日期:
打破
其他:
如果文件日期[0]。日期()<删除日期:
删除(podcastOut+仅限文件[迭代器])
迭代器+=1
欢迎提供任何建议。

检查“已下载”包含对
sys.exit()
的调用。这似乎是罪魁祸首。