📜  Python中的Pafy模块简介

📅  最后修改于: 2022-05-13 01:54:24.913000             🧑  作者: Mango

Python中的Pafy模块简介

在本文中,我们将了解 pafy 模块。 Pafy是一个Python库,用于下载 YouTube 内容和检索元数据。

安装
为了安装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