Python中的Pafy模块简介
在本文中,我们将了解 pafy 模块。 Pafy是一个Python库,用于下载 YouTube 内容和检索元数据。
Below are the list of features Pafy offers
1. Retrieve metadata such as viewcount, duration, rating, author, thumbnail, keywords
2. Download video or audio at requested resolution / bitrate / format / filesize
2. Command line tool (ytdl) for downloading directly from the command line
3. Retrieve the URL to stream the video in a player such as vlc or mplayer
4. Works with age-restricted videos and non-embeddable videos
5. Small, standalone, single importable module file
6. Select highest quality stream for download or streaming
7. Download video only (no audio) in m4v or webm format
8. Download audio only (no video) in ogg or m4a format
9. Retrieve playlists and playlist metadata
10. Works with Python 2.6+ and 3.3+
安装
为了安装pafy,我们使用下面给出的命令
pip install pafy
注意: Pafy 可以选择依赖于 youtube-dl,因此为了更稳定的使用,建议在安装 pafy 之前安装 youtube-dl。下面是安装 youtube-dl 的命令
pip install youtube_dl
示例 1:
获取视频观看次数的程序
Python3
# importing pafy
import pafy
# url of video
url = "https://www.youtube.com / watch?v = mmjDZGSr7EA"
# instant created
video = pafy.new(url)
# getting number of likes
count = video.viewcount
# showing likes
print("View Count : " + str(count))
Python3
# importing pafy
import pafy
# url of video
url = "https://www.youtube.com / watch?v = mmjDZGSr7EA"
# instant created
video = pafy.new(url)
# getting thumb image
count = video.thumb
# showing likes
print("Thumb Image : " + str(count))
Python3
# importing pafy
import pafy
# url of video
url = "https://www.youtube.com / watch?v = vG2PNdI8axo"
# instant created
video = pafy.new(url)
# getting title
value = video.title
# showing likes
print("Title : " + str(value))
输出 :
View Count : 287205
示例 2:
获取视频缩略图的程序
Python3
# importing pafy
import pafy
# url of video
url = "https://www.youtube.com / watch?v = mmjDZGSr7EA"
# instant created
video = pafy.new(url)
# getting thumb image
count = video.thumb
# showing likes
print("Thumb Image : " + str(count))
输出 :
Thumb Image : http://i.ytimg.com/vi/vG2PNdI8axo/default.jpg
示例 3:
获取视频标题的程序
Python3
# importing pafy
import pafy
# url of video
url = "https://www.youtube.com / watch?v = vG2PNdI8axo"
# instant created
video = pafy.new(url)
# getting title
value = video.title
# showing likes
print("Title : " + str(value))
输出 :
Title : DSA Self Paced Course | GeeksforGeeks