I am trying to program a script that downloads songs from YouTube, but when I want to get first result of the YouTube search I get this error:
AttributeError: 'Response' object has no attribute 'html
How can I solve this?
This is my code:
import os
import sys
import requests
from pytube import YouTube
# Check if a file name and a folder name were provided as command-line arguments
if len(sys.argv) != 3:
print("Usage: python download_songs.py <file name> <folder name>")
sys.exit(1)
# Get the file and folder names from the command-line arguments
file_name = sys.argv[1]
folder_name = sys.argv[2]
# Create the specified folder to save the downloaded songs
if not os.path.exists(folder_name):
os.makedirs(folder_name)# Prompt the user for the folder name
# Read the list of song titles from the input file
with open(file_name) as f:
song_titles = [line.strip() for line in f]
# Download each song from YouTube and save it to the specified folder
for song_title in song_titles:
url = "https://www.youtube.com/results?search_query=" + song_title + "extended mix"
# Send the request and get the response
response = requests.get(url)
# Parse the response and extract the first result
first_result = response.html.find("h3")[0]
# Get the link for the first result
link = first_result.a["href"]
# Use pytube to download the video at the link
yt = YouTube(link)
video = yt.streams.first()
video.download(folder_name)