To crawl thumbnails on YouTube, we use a interesting website that YouTube made for us for quick request for thumnail image.
1
| https://i.ytimg.com/vi/{YouTube_video_id}/maxresdefault.jpg
|
Firstly, we convert YouTube url to id by following code, and use “get” methed request the image.
Then, we stored the image in folder you like
using imagedown(YouTube_video_id, 'folder_name')
If there’s no such folder, then it will auto generate a new one.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import re import requests import os
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"
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')
|
Credits: StackoverFlow and jhnwr