首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Pytube库-尝试访问视频数据时收到"pytube.exceptions.RegexMatchError: regex pattern“错误

Pytube库-尝试访问视频数据时收到"pytube.exceptions.RegexMatchError: regex pattern“错误
EN

Stack Overflow用户
提问于 2019-01-04 03:32:06
回答 2查看 5K关注 0票数 3

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

正则表达式模式:正则表达式模式(yt.akamaized.net/)\s*||\s*.?\s_c\s*&&\s_d.set(^,+\s,\s*(?Pa-zA-Z0-9$+)()没有匹配项

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

代码语言:javascript
复制
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,但这并没有解决问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-01-04 11:36:29

pytube中有一个bug,所以使用这个命令重新安装它。

代码语言:javascript
复制
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

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

代码语言:javascript
复制
- 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。请检查此文件,以确保以下代码部分与您的代码部分匹配:

代码语言:javascript
复制
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)
票数 6
EN

Stack Overflow用户

发布于 2019-04-26 10:32:02

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54028675

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档