Python 3 Urllib to Extract YouTube Video ID From URL and Display it in Terminal
65 views
Jun 3, 2025
Get the full source code of application here: https://codingshiksha.com/python/python-3-urllib-to-extract-youtube-video-id-from-url-and-display-it-in-terminal/
View Video Transcript
0:00
uh hello guys uh welcome to this video
0:02
So in this video I will show you how to
0:04
extract uh the video ID from the YouTube
0:08
URL and simply return the video ID in
0:10
the terminal using Python So this is a
0:13
very common
0:14
task inside applications if you're
0:17
developing applications related to
0:21
uh let's suppose
0:23
uh YouTube So let's suppose let me uh
0:27
show you a very simple example So video
0:30
ID is nothing
0:32
but let me
0:35
just show you the video here Let's
0:37
suppose this is I'm taking this example
0:41
This is a YouTube video and this is
0:43
actually the video ID portion If you see
0:46
this is actually the video ID in the URL
0:50
So now if you want to extract this video
0:54
ID I will show you the Python script for
0:56
this So for this we will be using this
1:01
built-in module URL
1:04
lip So I will show you step by step the
1:09
script I've given the script in the
1:11
description of this video So just create
1:14
this app py file and now we just need to
1:17
import URL lib It contains this function
1:21
parse and from this we just need to
1:23
import URL parse comma and
1:27
parse
1:29
QoS and then after importing the
1:32
packages now we just need to simply make
1:35
the URL array So I will simply
1:40
paste the YouTube URL Simply write here
1:44
paste
1:46
it So we have pasted this Now we only
1:49
want this portion So how we get this for
1:52
this we define a actual
1:58
function
1:59
So we define a function here which will
2:03
extract YouTube video ID So here we
2:07
simply pass the URL
2:11
So instead of an array we can simply
2:15
just store this inside a simple variable
2:20
So now we just need to define this
2:22
function So right here I define this
2:28
extract So it takes the URL as an
2:31
argument
2:33
So in the function we actually do some
2:37
kind of regular expression First of all
2:40
we need to parse the URL
2:43
So for parsing the URL we actually use
2:46
this method URL parse and we pass the
2:50
URL to this function After that we now
2:54
need to actually make a if condition
2:58
that if
2:59
passed URL contains this property host
3:04
name and here we need to search to this
3:08
pattern
3:09
here www.youtube.com youtube.com or
3:14
youtube.com So if this URL matches any
3:18
of these patterns then we just need
3:20
to extract
3:22
the video id by using this function
3:26
parseq and here we simply pass parse url
3:30
dot query and then we return the query
3:35
dot
3:36
get the v portion So we need to extract
3:40
the portion after this v is equal to So
3:44
we are getting the value of v
3:50
uh like this and then we simply need to
3:53
say
3:56
none
3:59
zero So you can see that after
4:04
that we simply say
4:07
if we will basically do this for mobile
4:11
short URL as well or embed
4:17
URL but for the time period let me just
4:21
print out
4:25
this So just see if I execute the
4:32
script So it will print out this video
4:35
ID If you see that we printed out we
4:39
successfully extracted the video ID from
4:42
the URL So this is printing out right
4:44
here
4:47
So this is very simple Python script and
4:49
you can simply modify this to handle all
4:53
the URLs which are there in YouTube Let
4:58
me paste this The full script is given
5:00
in the description of this video So it
5:02
basically handles all the patterns This
5:05
is a normal URL This is the mobile URL
5:08
of YouTube This is the embed URL This is
5:12
the the long URL So in all the four
5:17
cases if I run this now it will return
5:19
out the video ID for each of the
5:21
patterns You will see first second third
5:25
fourth
5:26
So the script is given guys in the
5:29
description of this video and also check
5:32
out my website free media tools.com uh
5:35
which contains thousands of tools