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 ‘privacy’ key this will return privacy 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 privacy of item
value = metadata['privacy']
# printing value
print(value)
输出 :
public
另一个例子
# 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 privacy of item
value = metadata['privacy']
# printing value
print(value)
输出 :
public