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