gdata Python client library for Google data APIs
Google API. You can search YouTube videos
pytube Github: ablanco/python-youtube-download
Download YouTube videos
Let's search for videos from YouTube using gdata
# -*- coding: utf-8 -*-
#If you want to search in Japanese, enter the above tag
from gdata import *
import gdata.youtube
import gdata.youtube.service
search_word = "dog" # dogの動画を検索
client = gdata.youtube.service.YouTubeService()
#Create a search query
query = gdata.youtube.service,YouTubeVideoQuery()
query.vq = search_word #Search word
query.start_index = 1 #Which video to search from
query.max_results = 10 #How many video information do you want to get
query.racy = "exclude" #Whether to include the last video
query.orderby = "relevance" #What kind of order
#Perform a search and put the results in feed
feed = client.YouTubeQuery(query)
for entry in feed.entry:
#Extract the video link
#LinkFinder is
# from gdata import *
#Use from
link = LinkFinder.GetHtmlLink(entry)
print link
Execution result
<?xml version='1.0' encoding='UTF-8'?>
<ns0:link xmlns:ns0="http://www.w3.org/2005/Atom" href="https://www.youtube.com/watch?v=ZhCBEbsjdPo&feature=youtube_gdata" rel="alternate" type="text/html" />
.
.
.
I was able to get 10 links like this. Next, I will download the video referring to this link, but since all I need is the "href" part, let's cut it out.
#One of the link data acquired earlier
url = '<ns0:link xmlns:ns0="http://www.w3.org/2005/Atom" href="https://www.youtube.com/watch?v=wwAHyzfEEKc&feature=youtube_gdata" rel="alternate" type="text/html" />'
print url.split('href="')[1].split('&')[0]
#result>>> 'https://www.youtube.com/watch?v=wwAHyzfEEKc'
Note: If you want to search at once with for loop etc., max_results can be up to 50 and the number of loops can be up to 10. In other words, the maximum number of videos that can be searched at one time is 500.
from pytube import YouTube
yt = YouTube()
yt.url = 'https://www.youtube.com/watch?v=wwAHyzfEEKc'
#Run download
video = yt.get('mp4')
video.download('/path/to/videos/download/folder') #Download to your favorite folder
Execution result
I could easily download it like this. By combining the programs introduced this time, you can download up to 500 videos at a time.
Recommended Posts