尝试通过Python下载youtube字幕时,为什么会出现Regex错误?

时间:2019-04-24 16:10:47

标签: python api youtube youtube-api pytube

我正在尝试使用Youtube API或pytube(或通过任何方法)从youtube视频下载隐藏字幕(字幕)。

但是我不断得到这个: RegexMatchError:正则表达式模式(\ W [\'“]?t [\'”] ?:?\'“ [\'”])的匹配项为零

我不知道为什么会出现此错误;我使用了几种方法和代码,它们都有此Regex错误。

这很奇怪,因为几个星期前,我下载了youtube字幕,但现在不起作用。

为什么会出现此错误?

(我附带的代码来自 https://stackoverflow.com/search?q=youtube+captions+python

from pytube import YouTube

source = YouTube('https://www.youtube.com/watch?v=wjTn_EkgQRg&index=1&list=PLgJ7b1NurjD2oN5ZXbKbPjuI04d_S0V1K')


en_caption = source.captions.get_by_language_code('en')

en_caption_convert_to_srt =(en_caption.generate_srt_captions())

print(en_caption_convert_to_srt)
#save the caption to a file named Output.txt
text_file = open("Output.txt", "w")
text_file.write(en_caption_convert_to_srt)
text_file.close()

这是我的实际输出:

RegexMatchError                           Traceback (most recent call last)
<ipython-input-1-4b1a4cec5334> in <module>
      1 from pytube import YouTube
      2 
----> 3 source = YouTube('https://www.youtube.com/watch?v=wjTn_EkgQRg&index=1&list=PLgJ7b1NurjD2oN5ZXbKbPjuI04d_S0V1K')
      4 
      5 

c:\python\python37\lib\site-packages\pytube\__main__.py in __init__(self, url, defer_prefetch_init, on_progress_callback, on_complete_callback, proxies)
     86 
     87         if not defer_prefetch_init:
---> 88             self.prefetch_init()
     89 
     90     def prefetch_init(self):

c:\python\python37\lib\site-packages\pytube\__main__.py in prefetch_init(self)
     94 
     95         """
---> 96         self.prefetch()
     97         self.init()
     98 

c:\python\python37\lib\site-packages\pytube\__main__.py in prefetch(self)
    168             watch_html=self.watch_html,
    169             embed_html=self.embed_html,
--> 170             age_restricted=self.age_restricted,
    171         )
    172         self.vid_info = request.get(self.vid_info_url)

c:\python\python37\lib\site-packages\pytube\extract.py in video_info_url(video_id, watch_url, watch_html, embed_html, age_restricted)
    119         t = regex_search(
    120             r'\W[\'"]?t[\'"]?: ?[\'"](.+?)[\'"]', watch_html,
--> 121             group=0,
    122         )
    123         params = OrderedDict([

c:\python\python37\lib\site-packages\pytube\helpers.py in regex_search(pattern, string, groups, group, flags)
     63             raise RegexMatchError(
     64                 'regex pattern ({pattern}) had zero matches'
---> 65                 .format(pattern=pattern),
     66             )
     67         else:

RegexMatchError: regex pattern (\W[\'"]?t[\'"]?: ?[\'"](.+?)[\'"]) had zero matches

1 个答案:

答案 0 :(得分:0)

我也有这个问题。我使用了pip install pytubetemp并解决了它(我没有更改import语句)