Pafy – 获取每个播放列表项目的类别 ID
在本文中,我们将了解如何获取 pafy 中每个播放列表项的类别 ID。 Pafy 是一个Python库,用于下载 YouTube 内容和检索元数据。 Pafy 对象是包含有关给定视频的所有信息的对象。 YouTube 中的播放列表是一个按顺序播放的视频列表或一组视频,一个视频接一个视频。类别描述了与视频相关的内容。类别 ID 是每个类别的唯一 ID。
我们可以借助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 ‘category_id’ key this will return category id 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 category id of item
value = metadata['category_id']
# printing value
print("Category ID : " + str(value))
输出 :
Category ID : 22
另一个例子
# 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 category id of item
value = metadata['category_id']
# printing value
print("Category ID : " + str(value))
输出 :
Category ID : 27