Skip to content

Instantly share code, notes, and snippets.

@ryankert01
Created April 17, 2022 06:48
Show Gist options
  • Save ryankert01/edd774e2036c29d5903bdc95d9a1d501 to your computer and use it in GitHub Desktop.
Save ryankert01/edd774e2036c29d5903bdc95d9a1d501 to your computer and use it in GitHub Desktop.
downlaod youtube tumbnails image in the folder you like
import re
import requests
import os
#urls to id
url = "YouTube URL"
exp = "^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*"
s = re.findall(exp,url)[0][-1]
thumbnail = f"https://i.ytimg.com/vi/{s}/maxresdefault.jpg"
#image scraping
def imagedown(url, folder):
try:
os.mkdir(os.path.join(os.getcwd(), folder))
except:
pass
os.chdir(os.path.join(os.getcwd(), folder))
name = url
link = url
with open(name.replace(' ', '-').replace('/', '') + '.jpg', 'wb') as f:
im = requests.get(link)
f.write(im.content)
print('Writing: ', name)
imagedown(thumbnail, 'image')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment