我制作了这个程序,让用户输入他们想要下载的内容。然后,它会使用用户输入的关键字打开浏览器和 YouTube。用户可以将尽可能多的 URL 复制并粘贴到文本文件url.txt
中。只需保存文本文件,在 python shell 中按回车,即可开始下载。python有没有办法检测某个文件被关闭或保存,这样我就不必按Enter开始下载了?
import webbrowser
import pytube
from subprocess import call
key = input('Input what you want to download: ')
a_website = "https://www.youtube.com/results?search_query="+key
webbrowser.open_new(a_website)
open('url.txt', 'w')
call(['notepad', 'url.txt'])
start = input('Press enter when ready to download >>> ')
with open('url.txt','r') as f:
urls = f.readlines()
number = len(urls)
num = 0
for url in urls:
num += 1
print('Downloading video',num,'of',number)
yt = pytube.YouTube(url)
stream = yt.streams.first()
stream.download()
open('url.txt', 'w')