前言
Python有很多好用的库,在HTML文件处理上有一个常用的库dominate,能优雅地使用DOM API创建和操作HTML,只需要简单了解HTML语言的一些特性和Tag的意义,就能非常简洁地编写HTML页面。
HTML/CSS按钮
定义CSS按钮样式
.button_style {
background-color: #4CAF50; /* Green */
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}
HTML中使用CSS按钮样式
<a class="button button_style" href="test.html">test</a>
Python dominate使用按钮样式
import dominate
from dominate.tags import *
doc = dominate.document(title='test')
link = 'test.html'
with doc:
a('test', _class="button button_style", href=link)
-
需要注意的是:
dominate库可以使用关键字参数将属性附加到标签上,大多数属性都是来自HTML规范的直接拷贝,例如常见的href
、style
,
class
和for
由于是Python的关键字,所以不能直接用,如需使用则用如下别名:class: _class, cls, className, class_name
for: _for, fr, htmlFor, html_for
更多的CSS按钮样式
颜色、大小、圆角、边框、悬停、阴影、禁用、宽度、分组、动画的CSS按钮样式设置可以参考这里:“CSS 按钮”
参考资料:
[1] CSS 按钮
[2] HTML 标签
[3] HTML报告:python利用dominate库生成静态网页