尝试使用pytube API库访问数据(例如Youtube视频的“标题”)时,出现以下错误


  pytube.exceptions.RegexMatchError:regex模式(yt.akamaized.net/)\s*\|\|\s*.?\sc\s*&&\sd.set([^,]+\s,\s* (?P [a-zA-Z0-9 $] +)()具有零匹配


这是我正在使用的代码,上面提供了错误。

import sys
import pytube

link = input('Enter url: ') # url is: https://www.youtube.com/watch?v=KSbj-cFtFPA
yt = pytube.YouTube(link)
print(yt.title)

sys.exit()


可能有人知道如何解决此错误?我已经做了一些在线搜索,提供的答案不是确定的,并且在我尝试时不起作用。

注意:我尝试卸载pytube并重新安装它,但这不能解决问题。

最佳答案

pytube中有一个错误,因此请使用此命令重新安装。

pip install git+https://github.com/nficano/pytube.git

#####################################################

import sys
import pytube

link = input('Enter url: ') # url is: https://www.youtube.com/watch?v=KSbjcFtFPA
yt = pytube.YouTube(link)
print(yt.title)

**OUTPUT** Sanford and Son S02E02

sys.exit()


以下是有关此错误的一些其他信息:https://github.com/nficano/pytube/issues/333

这是对该问题的后续答复。我根据文档添加了一些错误处理,以告诉您发生了什么。

- Video 1 in the list will throw an extraction error. because the video has been removed from YouTube

- Video 2 in the list will throw an unavailable error. because the video does not exist on YouTube

- Video 3 in the list will display the correct title info, because the video exists on YouTube.

video_lists = ['https://www.youtube.com/watch?v=KSbjcFtFPA',
           'https://www.youtube.com/watch?v=MEEJOZkmIxvU',
           'https://www.youtube.com/watch?v=MEEJOZkmIxU'
           ]

for video in video_lists:

  try:
    yt = pytube.YouTube(video)
    print (yt.title)

  #################################################################
  # This one should catch - pytube.exceptions.RegexMatchError:
  # regex pattern ((?:v=|\/)([0-9A-Za-z_-]{11}).*) had zero matches
  #################################################################
  except pytube.exceptions.RegexMatchError:
    print('The Regex pattern did not return any matches for the video: {}'.format(video))

  except pytube.exceptions.ExtractError:
    print ('An extraction error occurred for the video: {}'.format(video))

  except pytube.exceptions.VideoUnavailable:
    print('The following video is unavailable: {}'.format(video))

**OUTPUTS**
An extraction occurred for the video: https://www.youtube.com/watch?v=KSbjcFtFPA

The following video is unavailable: https://www.youtube.com/watch?v=MEEJOZkmIxvU

Love of my life & Bohemian Rhapsody - 1080 HD


特别说明

有问题的错误已链接到pytube的cipher.py文件。请检查此文件,以确保下面的代码部分与您的匹配:

def get_initial_function_name(js):
"""Extract the name of the function responsible for computing the signature.

:param str js:
    The contents of the base.js asset file.

"""
# c&&d.set("signature", EE(c));
pattern = r'\bc\s*&&\s*d\.set\([^,]+\s*,\s*\([^)]*\)\s*\(\s*(?P<sig>[a-zA-Z0-9$]+)\('
logger.debug('finding initial function name')
return regex_search(pattern, js, group=1)

10-07 12:43