Python Youtube订阅mp3

Python Youtube订阅mp3,python,xml-parsing,youtube-dl,opml,Python,Xml Parsing,Youtube Dl,Opml,所以我的目标是编写代码,这样它就能自动从我订阅的所有Youtube频道下载到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

所以我的目标是编写代码,这样它就能自动从我订阅的所有Youtube频道下载到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)
        
导入opml
导入feedparser
导入youtube\u dl
从全局导入全局
从pprint导入pprint
从时间导入时间、mktime、strtime
从日期时间导入日期时间
如果len(glob('last.txt'))==0:
f=打开('last.txt','w')
f、 写入(str(time()))
打印('已初始化时间戳的最后一个.txt文件')
f、 关闭()
其他:
f=打开('last.txt','r')
content=f.read()
f、 关闭()
outline=opml.parse('subs.xml')
ptime=datetime.utcfromtimestamp(浮动(内容))
ftime=时间()
URL=[]
对于范围(0,len)(轮廓[0])中的i:
追加(大纲[0][i].xmlUrl)
打印(URL)
视频=[]
对于范围(0,len(URL))中的i:
print('通过通道'+str(i+1)+'解析'+str(len(url)),end='\r')
feed=feedparser.parse(URL[i])
对于范围内的j(0,len(提要['items']):
timef=feed['items'][j]['published\u parsed']
dt=日期时间.fromtimestamp(mktime(timef))
如果dt>ptime:
附加(feed['items'][j]['link'])
如果len(视频)==0:
打印('抱歉,找不到新视频')
其他:
打印(str(镜头(视频))+“找到了bew vudeis”)
ydl_选项={
“忽略错误”:True,

“格式”:“bestaudio[filesize错误显示您无权访问该文件。
如果我在我的电脑上运行
print(opml.parse('subs.xml'))
,我会得到完全相同的错误消息。 路径错误或您没有该文件的读取权限

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

outline=opml.parse(r'C:\folder\u name\subs.xml')

我觉得我们缺少了你的部分代码。这就是你为这个项目提供的全部代码吗?我现在添加了代码的开头,我以前无法发布它,因为它说代码太多,需要更多的解释,但这是一个错误,超出了我的知识和我所做的研究。它总是在同一个文件夹中,我尝试了你的方法:
outline=opml.parse(r'C:\Users\sound\Desktop\PythonProjets\subs.xml')
我仍然有这个错误:
OSError:error读取文件“C:\Users\sound\Desktop\PythonProjets\subs.xml”:无法加载外部实体“文件:/C:/Users/sound/Desktop/PythonProjets/subs.xml”
那么您可能对该文件有读取问题。您能将该文件放在另一个位置并重试吗?我确实交换到了D磁盘,但仍然存在相同的错误。
OSError:读取文件“D:\subs.xml”时出错:无法加载外部实体“file:/D:/subs.xml”
请尝试以下操作。在项目文件夹中创建一个新的.py文件,将youtubesubscriptions.py放置在该文件夹中并放入以下位置:
导入os
打印(os.access(r'C:\Users\sound\Desktop\PythonProjets\subs.xml',2))
当然要确保文件确实在那个地方。这会检查您是否对该文件有读取权限。请告诉我它返回了什么。所以我现在设法通过了。所有的坏问题似乎都是我的xml文件在'name.xml'中已经是xml了:D
TypeError:\uu init\uuuuuuuuo()收到一个意外的关键字参数audioquality
无法向您提出更多要求。我将继续并尝试启动我的项目。谢谢@FrozenAra
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"