如何python获取youtube视频的文本?

首先要安装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
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值