if query.hostname == 'youtu.be':
    return query.path[1:]
if query.hostname in ('www.youtube.com', 'youtube.com'):
    if query.path == '/watch':
        p = parse_qs(query.query)
return p['v'][0]
if query.path[:7] == '/embed/':
    return query.path.split('/')[2]
if query.path[:3] == '/v/':
    return query.path.split('/')[2]
return None
How can I extract video ID from YouTube's link in Python?