1
当我键入下面的代码时,它给了我一个空白的HTML页面。即使我把<h1>
和<a href>
标签。只有<title>
标签被执行。有谁知道为什么以及如何解决它?python文件中的HTML字符串没有按预期执行
代码:
my_variable = '''
<html>
<head>
<title>My HTML File</title>
</head>
<body>
<h1>Hello world!</h1>
<a href="https://www.hipstercode.com" target = "_blank">Click me</a>
</body>
</html>'''
my_html_file = open(r"\Users\hp\Desktop\Code\Python testing\CH\my_html_file.html", "w")
my_html_file.write(my_variable)
提前感谢!
这可能是因为你没有正确关闭文件。尝试'打开(...)为my_html_file:my_html_file.write(my_variable)'。 –
它适用于我。请注意,我为html文件使用了不同的名称。我看到标题,h1标题和链接。 –
你是如何打开html文件的? –