Pafy – 为播放列表的每个项目创建时间
在本文中,我们将了解如何获取 pafy 中每个播放列表项的时间创建值。 Pafy 是一个Python库,用于下载 YouTube 内容和检索元数据。 Pafy 对象是包含有关给定视频的所有信息的对象。 YouTube 中的播放列表是一个按顺序播放的视频列表或一组视频,一个视频接一个视频。
我们可以借助get_playlist
方法从 youtube 中获取播放列表,下面是执行此操作的命令
pafy.get_playlist(url)
播放列表 url 应该存在于 youtube 上,因为它会获取 youtube 上存在的那些视频的信息。 YouTube 是美国的在线视频分享平台。
Steps for implementation
1. Import the pafy module
2. Get the playlist with the help of URL of playlist
3. Return play list work as dictionary so use ‘items’ as key with the return playlist
4. Store the result in variable and from the items select the single item
5. With the single item use ‘playlist_meta’ key with it to get the meta data
6. With the meta data use ‘time_created’ key this will return time created value for given item
下面是实现
# importing pafy
import pafy
# url of playlist
url = "https://www.youtube.com / playlist?list = PLqM7alHXFySGqCvcwfqqMrteqWukz9ZoE"
# getting playlist
playlist = pafy.get_playlist(url)
# getting playlist items
items = playlist["items"]
# selecting single item
item = items[1]
# getting pafy object
i_pafy = item['pafy']
# getting meta data
metadata = item['playlist_meta']
# getting time created of item
value = metadata['time_created']
# printing value
print("Time Created : " + str(value))
输出 :
Time Created : 1490874673
另一个例子
# importing pafy
import pafy
# url of playlist
url = "https://www.youtube.com / playlist?list = PLqM7alHXFySE71A2bQdYp37vYr0aReknt"
# getting playlist
playlist = pafy.get_playlist(url)
# getting playlist items
items = playlist["items"]
# selecting single item
item = items[2]
# getting meta data
metadata = item['playlist_meta']
# getting time created of item
value = metadata['time_created']
# printing value
print("Time Created : " + str(value))
输出 :
Time Created : 1506577926