Created
January 29, 2014 11:35
-
-
Save oust/8686229 to your computer and use it in GitHub Desktop.
PythonでYouTubeの動画を自動的に検索&ダウンロードする ref: http://qiita.com/u651601f/items/1323ebe67ac0b4a38766
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" /> | |
. | |
. | |
. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 先ほど取得したリンクデータの1つ | |
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] | |
# 結果 >>> 'https://www.youtube.com/watch?v=wwAHyzfEEKc' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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