0

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

但我不断得到这个: RegexMatchError: regex pattern (\W[\'"]?t[\'"]?: ?\'"[\'"]) 有零匹配

我不知道为什么会出现这个错误;我使用了几种方法和代码,它们都有这个正则表达式错误。

这很奇怪,因为几周前,我下载了 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
4

1 回答 1

0

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

于 2019-04-25T13:03:04.087 回答