'How to scrape the thumbnail of a youtube video from id?
I am very new to python, I'm wondering how I would just get the maxres thumbnail to print, instead of literally everything about the video. Sorry if the answer is a bit obvious, I am a bit new to python and programming in general.
from apiclient.discovery import build
DEVELOPER_KEY = 'xxxxxx'
youtube = build('youtube', 'v3', developerKey=DEVELOPER_KEY)
ids = '6Gw-RyTRMns'
results = youtube.videos().list(id=ids, part='snippet').execute()
for result in results.get('items', []):
print(results)
I'm also wondering if there is a way to grab a URL to a picture from a description of a given video and have it saved to a folder.
Solution 1:[1]
This has been made easy thanks to YouTube. They use links like the one below to retreive thumbnails at different resolutions. Assuming all you need is the URL, just format a string like this:
http://img.youtube.com/vi/<insert-youtube-video-id-here>/maxresdefault.jpg
You can find a similar question here: How do I get a YouTube video thumbnail from the YouTube API?
You can find more information on the Api here: https://developers.google.com/youtube/v3/
To iterate over a channel you can find another similar question here: python: get all youtube video urls of a channel
To download the images you could use something like this.
import urllib
urllib.urlretrieve("http://img.youtube.com/vi/ytvideo/0.jpg")
Solution 2:[2]
For those reading this answer now, the updated url method is:
http://img.youtube.com/vi/<insert-youtube-video-id-here>/hqdefault.jpg
Solution 3:[3]
Here is a simple code. The regular expression matches all the shared and embedded URLs
import re
url = # Any 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"
print(thumbnail)
Solution 4:[4]
Add os.chdir
to manipulate directories to store thumnails.
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')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Community |
Solution 2 | Yaakov Bressler |
Solution 3 | |
Solution 4 | Ryan |