Automatically search and download YouTube videos with Python

Library to use

gdata Python client library for Google data APIs

Google API. You can search YouTube videos

pytube Github: ablanco/python-youtube-download

Download YouTube videos

Search 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&amp;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&amp;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.

Download youtube videos

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

Downloads-2.png

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

Automatically search and download YouTube videos with Python
Search and play YouTube videos in Python
How to download youtube videos with youtube-dl
Easily download mp3 / mp4 with python and youtube-dl!
Sequential search with Python
Causal reasoning and causal search with Python (for beginners)
Binary search with python
Binary search with Python3
Download and import files with Splunk external python
Get media timeline images and videos with Python + Tweepy
Easy partial download of mp4 with python and youtube-dl!
Get comments on youtube Live with [python] and [pytchat]!
Crawling with Python and Twitter API 1-Simple search function
Automatically translate DeepL into English with Python and Selenium
Programming with Python and Tkinter
Encryption and decryption with Python
Python and hardware-Using RS232C with Python-
Get Youtube data with python
YouTube video management with Python 3
Homebrew Python --Youtube Search Program
Full bit search with Python
python with pyenv and venv
Search engine work with python
Search twitter tweets with python
[Python] Depth-first search and breadth-first search
Download csv file with python
Streamline web search with python
Works with Python and R
Solving with Ruby and Python AtCoder ABC151 D Breadth-first search
How to convert Youtube to mp3 and download it super-safely [Python]
Create youtube ad auto skip tool with python and OCR
How to log in to AtCoder with Python and submit automatically
Communicate with FX-5204PS with Python and PyUSB
Shining life with Python and OpenCV
Robot running with Arduino and python
Install Python 2.7.9 and Python 3.4.x with pip.
Neural network with OpenCV 3 and Python 3
AM modulation and demodulation with python
[Python] font family and font with matplotlib
Scraping with Node, Ruby and Python
Implemented file download with Python + Bottle
Scraping with Python, Selenium and Chromedriver
Learn search with Python # 2bit search, permutation search
Scraping with Python and Beautiful Soup
JSON encoding and decoding with python
Hadoop introduction and MapReduce with Python
[GUI with Python] PyQt5-Drag and drop-
Reading and writing NetCDF with Python
I played with PyQt5 and Python3
Automatically build Python documentation with Sphinx
Reading and writing CSV with Python
Multiple integrals with Python and Sympy
Coexistence of Python2 and 3 with CircleCI (1.0)
Easy modeling with Blender and Python
Sugoroku game and addition game with python
Upload and download images with falcon
FM modulation and demodulation with Python
Python 2-minute search and its derivation
Crawling with Python and Twitter API 2-Implementation of user search function
Make blur videos look like fixed-point cameras with Python and OpenCV
Communicate between Elixir and Python with gRPC