【发布时间】:2022-01-21 14:39:16
【问题描述】:
你可以在这个 google colab 文件中运行我的代码 --> https://colab.research.google.com/drive/1Tfoa5y13GPLxbS-wFNmZpvtQDogyh1Rg?usp=sharing
所以我编写了一个脚本,它采用 YouTube 视频的 VideoID,例如:
VideoID = '3c584TGG7jQ'
基于此 VideoID,我的脚本返回带有 youtube 成绩单(视频内容)的字典列表,例如:
data = [{'text': 'Hello World', 'start': 0.19, 'duration': 4.21}, ...]
最后我写了一个函数,它接受用户的输入,即你要搜索的单词/句子,函数返回带有相应超链接的时间戳。
def search_dictionary(user_input, dictionary):
MY_CODE_SEE_GOOGLE_COLAB_NOTEBOOK
search_dictionary(user_input, dictionary)
Input: "stolen"
Output:
the 2 million packages that are stolen... 0.0 min und 39.0 sec :: https://youtu.be/3c584TGG7jQ?t=38s
stolen and the fifth is this outer... 3.0 min und 13.0 sec :: https://youtu.be/3c584TGG7jQ?t=192s
现在是我的问题。如何将此应用于 video_ids 列表?例如
list_of_video_ids = ['pXDx6DjNLDU', '8HEfIJlcFbs', '3c584TGG7jQ', ...]
预期输出:
Title_0, timestamp, hyperlink
Title_0, timestamp, hyperlink
Title_1, timestamp, hyperlink
Title_2, timestamp, hyperlink
Title_2, timestamp, hyperlink
Title_2, timestamp, hyperlink
Title_2, timestamp, hyperlink
所以所有 video_id 中的每一个提及,而不仅仅是一个 video_id
【问题讨论】:
-
您可以循环
list_of_video_ids中的字符串,并在每个循环中将transcript.fetch()的响应附加到您的data变量中。搜索如何每次都附加结果。 -
@MarcoAurelioFernandezReyes 您能否将我的代码复制粘贴到您自己的 google colab 中并应用您的解决方案?我有点卡住了。我还认为循环它会很容易。
-
不过,我仍在学习 Python,但是,当我阅读您的代码时,您几乎掌握了它。如果未将元素附加到给定的列表/字典,请尝试循环响应并在单独的列表变量中添加结果。也许我可以再过一段时间,但现在不行,抱歉。
-
好吧,没问题。慢慢来。
-
感谢您分享您的代码,我已对其进行了更改,希望对您有所帮助。干杯。
标签: python python-3.x youtube youtube-api