介绍
Aspose.HTML FOSS for Python 是一个用于在 Python 中处理 HTML 文档的新开源库。它将标记解析为基于标准的文档对象模型,允许您以编程方式构建和修改元素树,并解析 CSS —— 包括特异性、内联声明和继承 —— 为可从任何元素读取的计算样式。
该库面向浏览器不可用或不需要的后端和工具场景:清理已存储的 HTML 的服务、重构页面片段的流水线以及对生成的标记进行断言的测试套件。它可从 PyPI 安装为 aspose-html-foss,采用 MIT 许可证,并使用纯 Python 为 Python 3.10 及更高版本实现,因此在开发者机器、CI 运行器和服务器上表现一致。
除了 DOM 和 CSS 核心之外,该包还提供了实际的构建块,通常在 HTML 处理时必不可少:一个可直接驱动的 HTML 标记化器和树构建器、符合 WHATWG 风格的 URL 和 URLSearchParams 类,以及一个字节流编码检测器。
包含内容
构建和修改 DOM
DOM 层围绕 Document、Element 和 Node 构建。使用 Document.create_element() 创建元素,使用 append_child() 将它们连接,使用 Element.set_attribute() 设置标记属性,并使用 Document.get_element_by_id() 再次查找节点。完整页面和片段通过 HTMLDocument.parse() 和 HTMLDocument.parse_fragment() 进行解析。
from aspose_html.dom import Document
doc = Document()
el = doc.create_element("div")
el.set_attribute("class", "foo")
el.set_attribute("id", "bar")
doc.append_child(el)
应用样式表并读取计算样式
CSSStyleSheet.replace_sync() 解析样式表文本,Document.attach_style_sheet() 使其生效,Element.get_computed_style() 解析层叠。结果遵循选择器的特异性——在这里 ID 规则胜过类规则:
from aspose_html.dom import Document
from aspose_html.cssom import CSSStyleSheet
doc = Document()
el = doc.create_element("div")
el.set_attribute("class", "foo")
el.set_attribute("id", "bar")
doc.append_child(el)
sheet = CSSStyleSheet()
sheet.replace_sync(".foo { color: red } #bar { color: blue }")
doc.attach_style_sheet(sheet)
style = el.get_computed_style()
print(style.get_property_value("color"))
内联声明与层叠
每个元素通过其 style 属性公开一个实时的 CSSStyleDeclaration。使用 set_property() 设置的内联声明具有比作者样式表规则更高的特异性,get_computed_style() 体现了这一点:
from aspose_html.dom import Document
from aspose_html.cssom import CSSStyleSheet
doc = Document()
el = doc.create_element("div")
doc.append_child(el)
sheet = CSSStyleSheet()
sheet.replace_sync("div { color: red }")
doc.attach_style_sheet(sheet)
inline = el.style
inline.set_property("color", "blue")
print(el.get_computed_style().get_property_value("color"))
分词器与树构建器
当你需要比完整树更细粒度的控制时,底层提供公共的 API。Tokenizer.tokenize() 和 Tokenizer.tokenize_fragment() 为一段标记生成令牌流,TreeBuilder.run() 执行基于标准的树构建,而 parse_html() 函数则在一次调用中将标记转换为树。这些组件与高级解析路径使用的相同。
URL 与字符编码
URL.parse() 和 URL.can_parse() 处理 WHATWG 风格的 URL 解析和校验,URLSearchParams(append()、get()、has()、delete())用于查询字符串。对于原始字节,detect_encoding() 侦测编码——包括字节序标记——并返回解码后的文本以及置信度,而 get_canonical_name() 对编码标签进行规范化:
from aspose_html.encoding.detection import detect_encoding
result = detect_encoding(b"\xef\xbb\xbf<p>x</p>")
print(result.encoding)
print(result.confidence)
print(result.text)
快速入门
从 PyPI 安装:
asposefoss/html is not yet published — build from source until it ships. See the project README for build instructions.然后构建文档,为其设置样式,并读取计算结果:
from aspose_html.dom import Document
from aspose_html.cssom import CSSStyleSheet
doc = Document()
el = doc.create_element("div")
el.set_attribute("id", "bar")
doc.append_child(el)
sheet = CSSStyleSheet()
sheet.replace_sync("#bar { color: blue }")
doc.attach_style_sheet(sheet)
style = el.get_computed_style()
print(style.get_property_value("color"))
开源与许可
Aspose.HTML FOSS for Python 在 MIT 许可证下发布,因此允许商业使用、修改和再分发。源代码可在 GitHub 获取,软件包已在 PyPI 上以 aspose-html-foss 形式发布。