Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scrape the thumbnail of a youtube video from id?

Tags:

python

youtube

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.

like image 985
yevvy Avatar asked Oct 28 '16 02:10

yevvy


2 Answers

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")
like image 168
James Avatar answered Sep 28 '22 16:09

James


For those reading this answer now, the updated url method is:

http://img.youtube.com/vi/<insert-youtube-video-id-here>/hqdefault.jpg
like image 29
Yaakov Bressler Avatar answered Sep 28 '22 16:09

Yaakov Bressler