Skip to content

Instantly share code, notes, and snippets.

@oust
Created January 29, 2014 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oust/8686229 to your computer and use it in GitHub Desktop.
Save oust/8686229 to your computer and use it in GitHub Desktop.
PythonでYouTubeの動画を自動的に検索&ダウンロードする ref: http://qiita.com/u651601f/items/1323ebe67ac0b4a38766
# -*- coding: utf-8 -*-
# 日本語で検索したい場合は上のタグを入れる
from gdata import *
import gdata.youtube
import gdata.youtube.service
search_word = "犬" # 犬の動画を検索
client = gdata.youtube.service.YouTubeService()
# サーチクエリを作成
query = gdata.youtube.service,YouTubeVideoQuery()
query.vq = search_word # 検索ワード
query.start_index = 1 # 何番目の動画から検索するか
query.max_results = 10 # いくつの動画情報を取得したいか
query.racy = "exclude" # 最後の動画を含めるか
query.orderby = "relevance" # どんな並び順にするか
# 検索を実行し、feedに結果を入れる
feed = client.YouTubeQuery(query)
for entry in feed.entry:
# 動画のリンクを取り出す
# LinkFinderは、
# from gdata import *
# から使用
link = LinkFinder.GetHtmlLink(entry)
print link
<?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&amp;feature=youtube_gdata" rel="alternate" type="text/html" />
.
.
.
# 先ほど取得したリンクデータの1つ
url = '<ns0:link xmlns:ns0="http://www.w3.org/2005/Atom" href="https://www.youtube.com/watch?v=wwAHyzfEEKc&amp;feature=youtube_gdata" rel="alternate" type="text/html" />'
print url.split('href="')[1].split('&')[0]
# 結果 >>> 'https://www.youtube.com/watch?v=wwAHyzfEEKc'
from pytube import YouTube
yt = YouTube()
yt.url = 'https://www.youtube.com/watch?v=wwAHyzfEEKc'
# ダウンロードを実行
video = yt.get('mp4')
video.download('/path/to/videos/download/folder') # 好きなフォルダにダウンロードしてくれる
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment