Python 3 Script to Extract Youtube Video ID From URL Full Project For Beginners



# initial version: \
#    by Mikhail Kashkin(

def get_yt_video_id(url):
    """Returns Video_ID extracting from the given url of Youtube
    Examples of URLs:

    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 '' in query.hostname:
        return query.path[1:]
        raise ValueError

Leave a Reply