0

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.

7
  • 1
    Python has lots of string formatting methods that allow you to substitute the value of a variable into a string. Pick one and use it. Commented Jul 21, 2021 at 19:30
  • 1
    E.g. f-strings, the % operator, the str.format() method. Commented Jul 21, 2021 at 19:30
  • 1
    ...and please don't build products to pirate music. Lots of users here don't like helping people break the law / usage agreements / etc. Commented Jul 21, 2021 at 19:31
  • Use triple quotes for the multi-line HTML string. Commented Jul 21, 2021 at 19:31
  • @Xitiz I added php , so that if there's any method we can do this using php. Commented Jul 21, 2021 at 19:33

1 Answer 1

1

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}.

Sign up to request clarification or add additional context in comments.

6 Comments

Okayy , but then How do I deploy that HTML code when the program is running ??
What are you using to do deploy?
I hav'nt planned which service to use to deploy the website !!But at this moment I want to run the HTML file locally. How do I run the Html code when it is stored in a variable in python ??? ( I am using Pycharm)
@ChiragSingh check last edit. You can open that html file in browser and you will see title in download attribute!
You have to think about the flow here. This Python code will run on your web server. What's going to trigger that? Will the user be opening some web page? If so, then this code will need to be part of a web server application, like Django or Flask (or CGI). That opening trigger will cause your script to generate HTML back to the user's browser, where he can take the next step.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.