有人能告诉我为什么在python pytube脚本中得到“urllib.error.HTTPError:HTTP error 404:Not Found”?

2024-06-07 16:32:21 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有这个python pytube脚本来获取所有Youtube视频流

import sys
from pytube import YouTube

url = 'https://www.youtube.com/watch?v=XBkr9k9WmeI'
try:
    video = YouTube(url)
except:
    print("exception caught")
    sys.exit()

streams = video.streams.order_by('itag')

for st in streams:
    print(st, end=" ")

但是在运行这个脚本时,我在第streams = video.streams.order_by('itag')行上得到了这个错误urllib.error.HTTPError: HTTP Error 404: Not Found,我试图获取视频的itag排序的所有流,并将其存储在streams。 URL也是正确的,因为在video = Youtube(url)中,如果URL无效或此行无法执行,则不会捕获异常。 我在同一个项目文件夹中的另一个文件中也有相同的代码,但该脚本运行良好,我没有收到任何错误或任何东西。另一个代码如下:

def downloadYoutube(url, quality, path):
    #exception handling if the video link is valid or not
    try:
        video = YouTube(url)
    except:
        print("invalid url")
        return

    #getting the video title
    videoTitle= video.title+'.mp4'

    #getting all the atreams of video
    allStreams = video.streams.order_by('itag')

    #separately saving the progressive streams
    progrssiveStreams = allStreams.filter(progressive=True)

    # separately saving the non-progressive streams
    nonProgrossiveStreams = allStreams.filter(adaptive=True)

    # separately saving the mp4 format video streams
    mp4Streams = nonProgrossiveStreams.filter(mime_type='video/mp4')

这段代码运行得很好。在这段代码中,我可以过滤流并将其存储在不同的变量中,但上面的代码给出了一个错误。错误的屏幕截图如下:error screenshot

有人能帮我解释一下为什么我会犯这个错误吗


Tags: the代码脚本urlbyyoutubevideo错误

热门问题