python - Youtube-dl 订阅 mp3

标签 python xml-parsing youtube-dl opml

所以我的目标是编写代码,以便它可以自动从我订阅的所有 Youtube channel 下载到 mp3 文件。 我很难处理 EO 错误,这对我来说并不清楚,因此我从来没有处理过它,我已经做了研究,但没有什么能帮助我,所以这里是代码:

import opml
import feedparser
import youtube_dl
from glob import glob
from pprint import pprint

from time import time, mktime, strptime
from datetime import datetime

if len(glob('last.txt')) == 0:
    f = open ('last.txt' , 'w')
    f.write(str(time()))
    print('Initialized last.txt file for timestamp')
    f.close()
else:
    f = open('last.txt' , 'r')
    content = f.read()
    f.close()
    
    outline = opml.parse('subs.xml')
    
    ptime = datetime.utcfromtimestamp(float(content))
    ftime = time()
    urls = []
    for i in range(0,len(outline[0])):
        urls.append(outline[0][i].xmlUrl)
    print(urls)
    
    videos = []
    for i in range(0,len(urls)):
        print('Parsing through channel '+str(i+1)+' out of '+str(len(urls)), end='\r')
        feed = feedparser.parse(urls[i])
        for j in range(0,len(feed['items'])):
            timef = feed['items'][j]['published_parsed']
            dt = datetime.fromtimestamp(mktime(timef))
            if dt > ptime:
                videos.append(feed['items'][j]['link'])
                
    if len(videos) == 0:
        print('Sorry, no new video found')
    else:
        print(str(len(videos))+' bew vudeis found')
        
    ydl_options = {
            'ignoreerrors' : True,
            'format': 'bestaudio[filesize<30]',
            'keepvideo': False,
            'outtmpl': 'filename',
            'postprocessors': [{
                    'key': 'FFmpegExtractAudio',
                    'audioquality': '0',
                    'preferredquality': '320',
            }]
    }
     
    with youtube_dl.YoutubeDL(ydl_options) as ydl:
        ydl.download(videos)
        

我尝试了新的 YoutubeManager subs.xml ,尝试了其他具有不同 channel 的 Youtube 帐户,但他们的 subs.xml 没有任何帮助。

这是我的错误输出

runfile('C:/Users/sound/Desktop/PythonProjets/youtubesubscriptions.py', wdir='C:/Users/sound/Desktop/PythonProjets')
Traceback (most recent call last):

  File "<ipython-input-1-ff8a84b96d09>", line 1, in <module>
    runfile('C:/Users/sound/Desktop/PythonProjets/youtubesubscriptions.py', wdir='C:/Users/sound/Desktop/PythonProjets')

  File "C:\Users\sound\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
    execfile(filename, namespace)

  File "C:\Users\sound\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/sound/Desktop/PythonProjets/youtubesubscriptions.py", line 29, in <module>
    outline = opml.parse('subs.xml')

  File "C:\Users\sound\Anaconda3\lib\site-packages\opml\__init__.py", line 67, in parse
    return Opml(lxml.etree.parse(opml_url))

  File "src/lxml/etree.pyx", line 3435, in lxml.etree.parse

  File "src/lxml/parser.pxi", line 1840, in lxml.etree._parseDocument

  File "src/lxml/parser.pxi", line 1866, in lxml.etree._parseDocumentFromURL

  File "src/lxml/parser.pxi", line 1770, in lxml.etree._parseDocFromFile

  File "src/lxml/parser.pxi", line 1163, in lxml.etree._BaseParser._parseDocFromFile

  File "src/lxml/parser.pxi", line 601, in lxml.etree._ParserContext._handleParseResultDoc

  File "src/lxml/parser.pxi", line 711, in lxml.etree._handleParseResult

  File "src/lxml/parser.pxi", line 638, in lxml.etree._raiseParseError

OSError: Error reading file 'subs.xml': failed to load external entity "subs.xml"

最佳答案

错误表明您无权访问该文件。
如果我在我的 PC 上运行 print(opml.parse('subs.xml')) 我会收到完全相同的错误消息。 路径错误或您没有该文件的读取权限。

您的代码设置方式意味着 python 在您运行 .py 文件的路径中查找该文件。
subs.xml 是否与您的 python 文件位于同一文件夹中?
您可以尝试的一种方法是像这样直接链接路径:
outline = opml.parse(r'C:\folder_name\subs.xml')

关于python - Youtube-dl 订阅 mp3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64136615/

相关文章:

python - 在 ubuntu docker 镜像上连接时,SQL Server 的 ODBC 驱动程序 13 无法在 pyodbc 上打开 lib

javascript - 在 JS 中创建、编辑和保存 XML

python - 从 YouTube-DL 下载的音频文件已损坏

python - 在 django 中结合 Create 和 DetailView

python - 如何确保子类是数据类?

python - cv2.approxPolyDP() , cv2.arcLength() 这些是如何工作的

sql - 使用 SQL Server 从复杂的 XML 结构中读取值

xml - 创建不带结束标记的 XML 元素

audio - 如何将视频文件与音频文件合并并保持创建时间?

youtube - 有没有办法从YouTube视频中备份注释?