📜  在Python中使用 YouTube API 获取 YouTube 视频的观看次数、喜欢次数和标题的 GUI

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

在Python中使用 YouTube API 获取 YouTube 视频的观看次数、喜欢次数和标题的 GUI

先决条件: YouTube API、Tkinter

Python为开发 GUI(图形用户界面)提供了多种选择。在所有 GUI 方法中,Tkinter 是最常用的方法。它是Python附带的 Tk GUI 工具包的标准Python接口。 Python with Tkinter 是创建 GUI 应用程序的最快、最简单的方法。

在本文中,我们将学习如何使用Python中的 YouTube API 从任何 YouTube 视频中获取数据(如标题、观看次数、喜欢、不喜欢等)。

下面是要创建的 GUI:

循序渐进的方法:

在继续之前,应该正确地通过 YouTube API 以安装所需的模块并启用文章中提到的所需功能。

  • 导入所需的模块。
Python3
# Import Module
from tkinter import *
from googleapiclient.discovery import build


Python3
# creating youtube resource object 
youtube = build('youtube','v3', developerKey="Enter API Key") 
  
# retrieve youtube video results 
video_request=youtube.videos().list(
  part='snippet,statistics',
  id="Enter Video ID"
)
  
video_response = video_request.execute()


Python3
title = video_response['items'][0]['snippet']['title']
likes = video_response['items'][0]['statistics']['likeCount']
views = video_response['items'][0]['statistics']['viewCount']


Python3
# Create Object
root = Tk()
  
# Set Geometry
root.geometry("500x300")
  
# Add Label, Entry Box and Button
Label(root, text="Title, Views, Likes of YouTube Video", fg="blue",
      font=("Helvetica 20 bold"), relief="solid", bg="white").pack(pady=10)
Label(root, text="Enter video URL or ID", font=("10")).pack()
  
video_url = Entry(root, width=40, font=("15"))
video_url.pack(pady=10)
  
Button(root, text="Get Details", font=("Helvetica 15 bold")).pack()
  
details = Label(root, text="")
details.pack(pady=10)
  
# Execute Tkinter
root.mainloop()


Python3
# Import Module
from tkinter import *
from googleapiclient.discovery import build
  
def video_details():
    if "youtube" in video_url.get():
        video_id = video_url.get()[len("https://www.youtube.com/watch?v="):]
    else:
        video_id = video_url.get()
  
    # creating youtube resource object 
    youtube = build('youtube','v3',developerKey='Enter API Key')
  
    # retrieve youtube video results
    video_request=youtube.videos().list(
        part='snippet,statistics',
        id=video_id
    )
  
    video_response = video_request.execute()
  
    title = video_response['items'][0]['snippet']['title']
    likes = video_response['items'][0]['statistics']['likeCount']
    views = video_response['items'][0]['statistics']['viewCount']
  
    details.config(text=f"Title:- {title}\nLikes:- {likes}\nViews:- {views}")
  
# Create Object
root = Tk()
  
# Set Geometry
root.geometry("500x300")
  
# Add Label, Entry Box and Button
  
Label(root,text="Title, Views, Likes of YouTube Video", fg="blue",
    font=("Helvetica 20 bold"),relief="solid",bg="white").pack(pady=10)
Label(root,text="Enter video URL or ID", font=("10")).pack()
  
video_url = Entry(root,width=40,font=("15"))
video_url.pack(pady=10)
  
Button(root,text="Get Details" ,font=("Helvetica 15 bold"),command=video_details).pack()
  
details = Label(root,text="")
details.pack(pady=10)
  
# Execute Tkinter
root.mainloop()


  • 我们将使用build()list()execute()方法,它将提供视频详细信息。
  • 列表方法中,在部分属性中传递片段和统计信息,在videoId属性中传递VideoURLVideoID。

蟒蛇3

# creating youtube resource object 
youtube = build('youtube','v3', developerKey="Enter API Key") 
  
# retrieve youtube video results 
video_request=youtube.videos().list(
  part='snippet,statistics',
  id="Enter Video ID"
)
  
video_response = video_request.execute()
  • video_response 获取标题、观看次数、喜欢次数。

蟒蛇3

title = video_response['items'][0]['snippet']['title']
likes = video_response['items'][0]['statistics']['likeCount']
views = video_response['items'][0]['statistics']['viewCount']
  • 创建 Tkinter 窗口;添加按钮、标签等。

蟒蛇3

# Create Object
root = Tk()
  
# Set Geometry
root.geometry("500x300")
  
# Add Label, Entry Box and Button
Label(root, text="Title, Views, Likes of YouTube Video", fg="blue",
      font=("Helvetica 20 bold"), relief="solid", bg="white").pack(pady=10)
Label(root, text="Enter video URL or ID", font=("10")).pack()
  
video_url = Entry(root, width=40, font=("15"))
video_url.pack(pady=10)
  
Button(root, text="Get Details", font=("Helvetica 15 bold")).pack()
  
details = Label(root, text="")
details.pack(pady=10)
  
# Execute Tkinter
root.mainloop()

下面是基于上述方法的实现:

蟒蛇3

# Import Module
from tkinter import *
from googleapiclient.discovery import build
  
def video_details():
    if "youtube" in video_url.get():
        video_id = video_url.get()[len("https://www.youtube.com/watch?v="):]
    else:
        video_id = video_url.get()
  
    # creating youtube resource object 
    youtube = build('youtube','v3',developerKey='Enter API Key')
  
    # retrieve youtube video results
    video_request=youtube.videos().list(
        part='snippet,statistics',
        id=video_id
    )
  
    video_response = video_request.execute()
  
    title = video_response['items'][0]['snippet']['title']
    likes = video_response['items'][0]['statistics']['likeCount']
    views = video_response['items'][0]['statistics']['viewCount']
  
    details.config(text=f"Title:- {title}\nLikes:- {likes}\nViews:- {views}")
  
# Create Object
root = Tk()
  
# Set Geometry
root.geometry("500x300")
  
# Add Label, Entry Box and Button
  
Label(root,text="Title, Views, Likes of YouTube Video", fg="blue",
    font=("Helvetica 20 bold"),relief="solid",bg="white").pack(pady=10)
Label(root,text="Enter video URL or ID", font=("10")).pack()
  
video_url = Entry(root,width=40,font=("15"))
video_url.pack(pady=10)
  
Button(root,text="Get Details" ,font=("Helvetica 15 bold"),command=video_details).pack()
  
details = Label(root,text="")
details.pack(pady=10)
  
# Execute Tkinter
root.mainloop()

输出: