Pytube库-尝试访问视频数据时收到“ pytube.exceptions.RegexMatchError:正则表达式模式”错误

时间:2019-01-03 19:32:06

标签: python python-3.x

尝试使用pytube API库访问诸如YouTube视频“标题”之类的数据时,我收到以下错误消息

  

pytube.exceptions.RegexMatchError:正则表达式模式(yt.akamaized.net /)\ s * \ | \ | \\ s *。?\ s c \ s * && \ s d.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并重新安装它,但这不能解决问题。

2 个答案:

答案 0 :(得分:2)

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)

答案 1 :(得分:0)

@ Victor S:尝试安装pytube和pytubetemp,确保两者均为9.4.0。不要更改导入,请重新运行代码。