Last active
February 15, 2024 17:15
-
-
Save kmonsoor/2a1afba4ee127cce50a0 to your computer and use it in GitHub Desktop.
Extract Video-ID from a Youtube url
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# initial version: http://stackoverflow.com/a/7936523/617185 \ | |
# by Mikhail Kashkin(http://stackoverflow.com/users/85739/mikhail-kashkin) | |
def get_yt_video_id(url): | |
"""Returns Video_ID extracting from the given url of Youtube | |
Examples of URLs: | |
Valid: | |
'http://youtu.be/_lOT2p_FCvA', | |
'www.youtube.com/watch?v=_lOT2p_FCvA&feature=feedu', | |
'http://www.youtube.com/embed/_lOT2p_FCvA', | |
'http://www.youtube.com/v/_lOT2p_FCvA?version=3&hl=en_US', | |
'https://www.youtube.com/watch?v=rTHlyTphWP0&index=6&list=PLjeDyYvG6-40qawYNR4juzvSOg-ezZ2a6', | |
'youtube.com/watch?v=_lOT2p_FCvA', | |
Invalid: | |
'youtu.be/watch?v=_lOT2p_FCvA', | |
""" | |
from urlparse import urlparse, parse_qs | |
if url.startswith(('youtu', 'www')): | |
url = 'http://' + url | |
query = urlparse(url) | |
if 'youtube' in query.hostname: | |
if query.path == '/watch': | |
return parse_qs(query.query)['v'][0] | |
elif query.path.startswith(('/embed/', '/v/')): | |
return query.path.split('/')[2] | |
elif 'youtu.be' in query.hostname: | |
return query.path[1:] | |
else: | |
raise ValueError |
Very good, thank you.
Compatible with python 3
try:
# python 3
from urllib.parse import urlparse, parse_qs
except ImportError:
# python 2
from urlparse import urlparse, parse_qs
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is very useful for scraping YouTube links from forum comments. However, various forms of invalid
url
returnNone
, for exampleget_yt_video_id('youtube')
(a plain text mention of 'youtube') andget_yt_video_id('https://www.youtube.com/playlist?list=exampeofaplaylisturl')
. Perhapselse
statements could be used to raise a ValueError?