Skip to content

Instantly share code, notes, and snippets.

@XWilliamY
Created July 3, 2020 03:01
Show Gist options
  • Save XWilliamY/b8edf30eb8cf42858a3828e2735aeaa2 to your computer and use it in GitHub Desktop.
Save XWilliamY/b8edf30eb8cf42858a3828e2735aeaa2 to your computer and use it in GitHub Desktop.
from urllib.parse import urlparse, parse_qs
# adopted from:
# https://stackoverflow.com/questions/45579306/get-youtube-video-url-or-youtube-video-id-from-a-string-using-regex
def get_id(url):
u_pars = urlparse(url)
quer_v = parse_qs(u_pars.query).get('v')
if quer_v:
return quer_v[0]
pth = u_pars.path.split('/')
if pth:
return pth[-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment