pytube字幕的lauguage代码格式似乎已更改。
from pytube import YouTube
video_link = r'https://www.youtube.com/watch?v=w7daiJHfjoY'
yt = YouTube(video_link)
print(yt.captions)
现在的结果如下所示:
{'a.de': <Caption lang="German (auto-generated)" code="a.de">, 'de.CcQ45jRV4-E': <Caption lang="German - deutsch" code="de.CcQ45jRV4-E">}
在我可以提取字幕之前
yt.captions.get_by_language_code('de')
但是因为现在字幕的语言代码变为de.CcQ45jRV4-E,所以我需要使用 yt.captions.get_by_language_code('de.CcQ45jRV4-E')
虽然可以,但是我不知道该语言代码是否固定。如何使用字符串通配符在字幕中获取想要的字幕?就像是:
yt.captions.get_by_language_code('de*')
谢谢你。
遍历字幕:
from pytube import YouTube
video_link = r'https://www.youtube.com/watch?v=w7daiJHfjoY'
yt = YouTube(video_link)
for c in yt.captions:
if "de." in c.code:
caption = c
break
print(caption)
假设在“ de”之后总会有一个点。对于更复杂的匹配,请使用正则表达式,但我认为没有必要。