Python youtubesearchpython – Retrieve YouTube video information

In this post we will be creating a Python script that will retrieve information on a YouTube Video. To achieve this we will be using the youtubesearchpython library. The library allows us to search and get information about videos on YouTube using Python. It provides an easy-to-use interface for accessing the YouTube Data API v3 and parsing the JSON responses returned by the API.

See the sample of Python code below where we retrieve information for a given YouTube video. The video in this example is the YouTube URL given via input from the user.

We start by creating a Video object named video by retrieving information about the YouTube video with the URL We then return the relevant information stored as key-value pairs in the video object. The f before the string allows for the use of formatted string literals, which allow you to embed expressions inside string literals.

from youtubesearchpython import Video

video = Video.get("")
print(f"ID: {video['id']}")
print(f"Title: {video['title']}")
print(f"Duration: {video['duration']}")
print(f"Views: {video['viewCount']}")
print(f"Channel: {video['channel']}")
print(f"Thumbnails: {video['thumbnails']}")
print(f"Description: {video['description']}")

Take a look at some of our other content around the Python programming language by clicking here.

Leave a Reply