I have a String variable(name) that contains the name of the song. (Python)
from pytube import YouTube yt = YouTube("https://www.youtube.com/watch?v=6BYIKEH0RCQ") name = yt.title #Contains the title of the song
Here is my HTML code for website to download the mp3 song:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Download</title> </head> <body> <a href="Sample.mp3" download="Song_name"><button>Click here </button></a> </body> </html>
With this code, I’d like to use the exact title of song as the name of the file when its been downloaded from the user. I want to use the name Variable from Python in place of Song_name in HTML code.
Please suggest me any possible way in order to make this work.
Advertisement
Answer
You can try try this:
from pytube import YouTube yt = YouTube("https://www.youtube.com/watch?v=6BYIKEH0RCQ") name = yt.title HTML=f""" <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Download</title> </head> <body> <a href="Sample.mp3" download="{name}"><button>Click here </button></a> </body> </html> """ with open("test.html","w") as f: f.write(HTML)
This will put title of song in download attribute. If you want you may put it anywhere. Just don’t forget to use f""
and {variable}
.