Pafy – 获取播放列表项
在本文中,我们将了解如何在 pafy 中获取 youtube 播放列表项。 Pafy 是一个Python库,用于下载 YouTube 内容和检索元数据。 Pafy 对象是包含有关给定视频的所有信息的对象。 YouTube 中的播放列表是一个按顺序播放的视频列表或一组视频,一个视频接一个视频。当一个视频播放完毕后,下一个视频会自动开始播放,因此您无需单击或搜索即可开始播放新视频。播放列表项目是单个视频 总项目是播放列表中的视频总数。
我们可以借助get_playlist
方法从 youtube 中获取播放列表,下面是执行此操作的命令
pafy.get_playlist(url)
播放列表 url 应该存在于 youtube 上,因为它会获取 youtube 上存在的那些视频的信息。 YouTube 是美国的在线视频分享平台。
Steps to get the playlist ID
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 print it
下面是实现
# 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"]
# printing single item
print(items[1])
输出 :
{'pafy': Pafy object: AfxHGNRtFac [Write a program to print all permutations of ..], 'playlist_meta': {'added': '06/01/2016', 'is_cc': False, 'is_hd': True, 'likes': 1111, 'title': 'Write a program to print all permutations of a given string | GeeksforGeeks', 'views': '334, 905', 'rating': 4.0, 'author': 'GeeksforGeeks', 'user_id': '0RhatS1pyxInC00YKjjBqQ', 'privacy': 'public', 'start': 0.0, 'dislikes': 227, 'duration': '11:52', 'comments': '116', 'keywords': '', 'thumbnail': 'https://i.ytimg.com/vi/AfxHGNRtFac/default.jpg', 'cc_license': False, 'category_id': 22, 'description': 'Explanation for the Article: https://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/\n\nThis video is contributed by Sephiri.\n#geeksforgeeks', 'encrypted_id': 'AfxHGNRtFac', 'time_created': 1490874673, 'time_updated': None, 'length_seconds': 712, 'end': 712}}
另一个例子
# 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"]
# printing single item
print(items[2])
输出 :
{'pafy': Pafy object: GY-KULykGaw [Find subarray with given sum | Set 1 (Non-neg..], 'playlist_meta': {'added': '27/09/2017', 'is_cc': True, 'is_hd': True, 'likes': 192, 'title': 'Find subarray with given sum | Set 1 (Non-negative Numbers) | GeeksforGeeks', 'views': '32, 716', 'rating': 3.0, 'author': 'GeeksforGeeks', 'user_id': '0RhatS1pyxInC00YKjjBqQ', 'privacy': 'public', 'start': 0.0, 'dislikes': 78, 'duration': '2:50', 'comments': '24', 'keywords': 'GeeksforGeeks Programming Algorithms "Data Structures" Coding Tutorial array "non-negative numbers"', 'thumbnail': 'https://i.ytimg.com/vi/GY-KULykGaw/default.jpg', 'cc_license': False, 'category_id': 27, 'description': "Find Complete Code at GeeksforGeeks Article: https://www.geeksforgeeks.org/find-subarray-with-given-sum/\n\nPractice Problem Online Judge: http://practice.geeksforgeeks.org/problems/subarray-with-given-sum/0\n\nThis video is contributed by Shubham Kumar\n\nPlease Like, Comment and Share the Video among your friends.\n\nAlso, Subscribe if you haven't already! :)", 'encrypted_id': 'GY-KULykGaw', 'time_created': 1506577926, 'time_updated': None, 'length_seconds': 170, 'end': 170}}