怎样使用 Python 和 JavaScript 写一个简单的网站?
Python
# Create a basic HTML document
html = """
My Website
Hello, world!
"""
# Save the HTML document to a file
with open("index.html", "w") as f:
f.write(html)
# Open the HTML file in a web browser
# (This will open a new window/tab)
print("Your website is ready!")
JavaScript
// Get the HTML content of the page
const html = document.documentElement.outerHTML;
// Set the title of the page
document.title = "My Website";
// Create a heading element
const heading = document.createElement("h1");
heading.textContent = "Hello, world!";
// Append the heading element to the body of the page
document.body.appendChild(heading);
// Print a message to the console
console.log("Your website is ready!");
How to run the code:
- Save the Python file as
index.py
and the JavaScript file asindex.js
. - Open a terminal or command prompt in the same directory as the files.
- Run the Python file with the command:
python index.py
- Run the JavaScript file with the command:
node index.js
To use the website:
- Open a web browser and navigate to
localhost:8000
. - You should see the text "Hello, world!" displayed on the page.
Note:
- You can customize the HTML and JavaScript code to add more content and functionality to your website.
- You can also use a web framework like Django or Flask to build a more complex website with a database and routing.