Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

parse youtube video id from youtube link python

# 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
Comment

PREVIOUS NEXT
Code Example
Python :: set window size tkinter 
Python :: order by listview django 
Python :: when did guido van rossum create python 
Python :: tesseract.exe python 
Python :: learn python the hard way pdf 
Python :: python read gzipped file 
Python :: covariance matrix python 
Python :: standardize columns in pandas 
Python :: pandas drop rows with null in specific column 
Python :: django gmail smtp 
Python :: r2 score sklearn 
Python :: remove None pandas 
Python :: email validation python 
Python :: python calculate age from date of birth 
Python :: python name of current file 
Python :: remove multiple space python 
Python :: on_ready discord.py 
Python :: log scale seaborn 
Python :: brownie normalize to wei 
Python :: bar chart with seaborn 
Python :: yesterday in python 
Python :: python reciprocal 
Python :: wtf forms required 
Python :: python except show error 
Python :: multipl excel sheets in pandas 
Python :: python input separated by 
Python :: check odd numbers numpy 
Python :: how to know if python is 64 or 32 bit 
Python :: pandas df remove index 
Python :: python image read 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =