Last active
December 22, 2018 04:33
-
-
Save thiagodeschamps/611d12f6080c93f6b67124803afb1870 to your computer and use it in GitHub Desktop.
Python script that search and download videos from youtube
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 bs4 import BeautifulSoup | |
import requests | |
import pytube | |
import os | |
if not os.path.exists('videos'): | |
os.makedirs('videos') | |
os.chdir('videos') | |
video = str(input('Search: ')) | |
video = video.split(' ') | |
video = '+'.join(video) | |
url = f'https://www.youtube.com/results?search_query={video}' | |
response = requests.get(url).text | |
cont = BeautifulSoup(response, 'lxml') | |
body = cont.body | |
link = body.findAll('a', class_ = 'yt-uix-tile-link yt-ui-ellipsis yt-ui-ellipsis-2 yt-uix-sessionlink spf-link ') | |
for j, i in enumerate(link): | |
print(f'{j+1}: ', i['title']) | |
n = int(input('Choose the video: ')) | |
flink = link[n - 1] | |
print(flink['title']) | |
watch = str(flink['href']) | |
title = str(flink['title']) | |
path = f'http://www.youtube.com{watch}' | |
print(f'LINK: {path}') | |
print(f'Downloading {title}...') | |
yt = pytube.YouTube(path) | |
stream = yt.streams.first() | |
stream.download() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment