tinycss2 是一个 Python 库,它提供了一种解析 CSS 样式表的轻量级方法。与其他 CSS 解析库相比,tinycss2 具有更快的解析速度和更低的内存占用,同时支持 CSS3 和 CSS4 标准中的大多数特性。
以下是 tinycss2 库的一些常见用法:
- 解析 CSS 样式表
from tinycss2 import parse_stylesheet
# 解析样式表
css = '''
body {
background-color: #f0f0f0;
font-family: sans-serif;
}
h1, h2, h3 {
color: #333;
font-weight: bold;
}
'''
stylesheet = parse_stylesheet(css)
# 遍历样式规则
for rule in stylesheet: