python生成的html动态页面能添加视频或者图片显示,只给出python实现代码
时间: 2023-02-10 14:47:17 浏览: 98
可以使用 Python 中的 HTML 模板来生成动态 HTML 页面。
下面是一个简单的示例,使用 Python 生成一个包含视频和图片的 HTML 页面。
```
from string import Template
video_template = Template('''
<video width="640" height="480" controls>
<source src="$video_url" type="video/mp4">
Your browser does not support the video tag.
</video>
''')
image_template = Template('''
<img src="$image_url" alt="$image_description" width="640" height="480">
''')
html_template = Template('''
<html>
<body>
$video_html
$image_html
</body>
</html>
''')
video_html = video_template.substitute(video_url='https://example.com/video.mp4')
image_html = image_template.substitute(image_url='https://example.com/image.jpg', image_description='A beautiful image')
html = html_template.substitute(video_html=video_html, image_html=image_html)
with open('index.html', 'w') as f:
f.write(html)
```
在这个示例中,我们首先定义了两个字符串模板,一个是视频模板,另一个是图像模板。然后我们使用 `substitute` 方法替换变量的值,并生成包含视频和图像的 HTML 页面。最后,我们使用 `open` 函数写入文件。
你还可以使用 Python 的其他库,如 Jinja2,来生成动态 HTML 页面。