首先要安装youtube视频API包:youtube-transcript-api,可以使用一下命令:
pip install youtube-transcript-api
然后从网址获取视频ID:https://www.youtube.com/watch?v=l0RZ9OJBRnw,其中l0RZ9OJBRnw就是我们的ID
通过这两行命令,我们将获取字典列表
from youtube_transcript_api import YouTubeTranscriptApi
transcript = YouTubeTranscriptApi.get_transcript("l0RZ9OJBRnw")
字典列表如下(只展示一部分):
[{'text': 'hey everybody welcome back to my YouTube', 'start': 0.16, 'duration': 5.04}, {'text': "channel my name's", 'start': 2.72, 'duration': 5.48}]
可以发现字典中的keys有text(文本), start(开始时间点), duration(时长),通过len(transcript)获取列表长度
如果要只选取所有的text文本,执行一下命令:
text = []
for i in range(len(transcript)):
text.append(transcript[i]["text"])
得到text字符串列表,将所有的字符串合并:
text = " ".join(text)
print(text)
就得到我们需要的一大段字符串啦(只展示合并的前30个):
hey everybody welcome back to my YouTube channel my name's aringa and in today's video we're going to learn some English at the grocery store let's get started before going inside let's grab a shopping cart a shopping cart is probably better if you're going to buy a bunch of groceries it's easier to push the shopping cart than carry a heavy basket but if you are going to be in and out of the store and you're only going to buy a couple of items then I recommend you grabbing a basket rather than pushing a shopping cart right now I'm inside the grocery store and before coming to the grocery store I like to make a list of items that I want to buy or a grocery list this is a grocery list and it does come in handy because I can find things in the same aisles and I can make sure I want to buy the things on my list and not forget anything inside the grocery store you can find different sections of foods for example right now I'm in the produce section and in this section you can find vegetables and fruits they have different types of produce in the store they have organic and conventional