Skip to content

Instantly share code, notes, and snippets.

@tejastank
Forked from smellslikeml/yt_download.py
Created April 7, 2020 13:25
Show Gist options
  • Save tejastank/6d9a3da0767f0dfc36031d742a9e8305 to your computer and use it in GitHub Desktop.
Save tejastank/6d9a3da0767f0dfc36031d742a9e8305 to your computer and use it in GitHub Desktop.
download youtube videos to .mp4 with command line argument search string
#!/usr/bin/env python
import os
import sys
import requests
from bs4 import BeautifulSoup as bs
from urllib.parse import urlencode
from pytube import YouTube
qstring = sys.argv[1]
out_dir = sys.argv[2]
base = "https://www.youtube.com/results?"
s = {"search_query": qstring}
s = urlencode(s)
r = requests.get(base + s)
page = r.text
soup=bs(page,'html.parser')
vids = soup.findAll('a',attrs={'class':'yt-uix-tile-link'})
videolist=[]
for v in vids:
tmp = 'https://www.youtube.com' + v['href']
videolist.append(tmp)
count=0
os.chdir(out_dir)
for item in videolist:
# increment counter:
count+=1
try:
yt = YouTube(item)
# grab the video:
yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first().download()
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment