You can use Python along with the pytube
library for downloading YouTube videos and BeautifulSoup
for parsing HTML. First, make sure you have the required libraries installed:
pip install pytube beautifulsoup4
Here is a Python script that reads an HTML file containing YouTube links, downloads the videos, and creates a new HTML file to track the successfully downloaded videos:
import os
from bs4 import BeautifulSoup
from pytube import YouTube
def download_video(url, output_folder):
try:
yt = YouTube(url)
video = yt.streams.filter(file_extension='mp4', resolution='720p').first()
video.download(output_folder)
print(f"Downloaded: {yt.title}")
return True
except Exception as e:
print(f"Error downloading {url}: {e}")
return False
def process_html(input_html, output_folder):
# Create an output HTML file to track successful downloads
output_html_path = os.path.join(output_folder, "downloaded_videos.html")
with open(output_html_path, "w", encoding="utf-8") as output_html:
output_html.write("<html><body>\n")
# Parse the input HTML file
with open(input_html, "r", encoding="utf-8") as input_file:
soup = BeautifulSoup(input_file, "html.parser")
links = soup.find_all("a")
for link in links:
url = link.get("href")
if url and "youtube.com" in url:
success = download_video(url, output_folder)
if success:
output_html.write(f'<p><a href="{url}">{url}</a></p>\n')
output_html.write("</body></html>\n")
if __name__ == "__main__":
input_html_path = "path/to/your/input.html" # Replace with the path to your HTML file
output_folder_path = "path/to/your/documents/folder" # Replace with the path to your documents folder
if not os.path.exists(output_folder_path):
os.makedirs(output_folder_path)
process_html(input_html_path, output_folder_path)
Replace "path/to/your/input.html"
with the path to your HTML file containing YouTube links, and "path/to/your/documents/folder"
with the path to your documents folder.
mkdir pyver
mkdir pyproj
install to pyver\py3121
cd pyproj
c:\users\user\pyver\py3121\python -m venv youtube_downloads
tree youtube_downloads
from pyproj youtube_downloads\scripts\activate
cd pyproj
c:\users\user\pyver\py3121\python -m venv youtube_downloads
c:\users\user\pyproj youtube_downloads\scripts\activate
C:\Users\user\pyproj> python c:\users\user\pyproj\youtube_downloads\scripts\youtube_downloads.py