Aspose.HTML FOSS for Python 是一个免费、开源的库,用于在 Python 应用程序中处理 HTML 文档。使用单个 git clone https://github.com/aspose-html-foss/Aspose.HTML-FOSS-for-Python.git 命令进行安装,开始将标记解析为文档对象模型,构建和修改元素树,附加 CSS 样式表,解析计算后的样式,并处理 URL 和字符编码——全部使用纯 Python,无需浏览器依赖。
该库公开了一个以标准为导向的 API,围绕 HTMLDocument、Document、Element 和 CSSStyleSheet 构建。使用 HTMLDocument.parse() 解析标记,使用 Document.create_element() 和 append_child() 创建并连接节点,使用 get_element_by_id() 查找元素,通过 CSSStyleSheet.replace_sync() 以及元素级的 style 声明为它们设置样式,并使用 Element.get_computed_style() 读取结果。较低层的构建块——HTML Tokenizer、TreeBuilder、WHATWG 风格的 URL 和 URLSearchParams,以及 detect_encoding() 字节流检测器——在需要直接控制时可用。
Aspose.HTML FOSS 使用 MIT 许可证,纯 Python 实现,要求 Python 3.10 或更高版本。它在 Windows、Linux 和 macOS 上表现完全相同——包括 CI 运行器、Docker 容器和无服务器环境。有关企业产品系列,请参阅 Aspose.HTML — Enterprise Product Family。
HTMLDocument.parse() 解析完整页面,或使用 HTMLDocument.parse_fragment() 解析片段Document.create_element() 和 append_child() 以编程方式构建文档Document.get_element_by_id() 查找节点,并通过 Element.get_attribute() 读取标记属性Element.set_attribute() 设置属性,并通过 Node.text_content 赋予文本serialise() 函数将任意子树序列化回标记CSSStyleSheet.replace_sync() 解析样式表文本,并通过 Document.attach_style_sheet() 附加它style 属性使用 CSSStyleDeclaration.set_property() 设置内联声明Element.get_computed_style() 解析层叠——特异性、内联与作者规则以及继承——CSSStyleDeclaration.get_property_value() 读取解析后的值Tokenizer.tokenize() 或 Tokenizer.tokenize_fragment() 驱动 HTML 标记器TreeBuilder.run() 进行基于标准的树构建parse_html() 函数直接将标记解析为树URL.parse() 和 URL.can_parse() 解析并验证 URLURLSearchParams 读取和编辑查询字符串 — append()、get()、has()、delete()detect_encoding() 检测字节流编码(BOM 和内容嗅探)get_canonical_name() 将编码标签规范化为标准名称创建文档,附加样式表,并读取计算后的样式 — 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"))
内联声明的特异性高于作者样式表规则:
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"))
嗅探原始字节流(包括 BOM)的编码,并获取解码后的文本:
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)
MIT License — 商业使用、修改和再分发均被允许;唯一的要求是随大量复制品保留版权声明。
Python 3.10 或更高版本。使用 git clone https://github.com/aspose-html-foss/Aspose.HTML-FOSS-for-Python.git 安装 — 实现是纯 Python,没有本机依赖。
使用 HTMLDocument.parse() 处理完整页面,或使用 HTMLDocument.parse_fragment() 处理部分标记;Document 是用于以编程方式构建树的工厂。
是的 — 计算值遵循选择器特异性,内联声明优先于作者规则,!important 作者规则优先于非重要的内联声明,并且继承从父到子。
不。当前版本侧重于解析、DOM 操作、CSS 样式计算、布局结构、URL 和编码——不进行格式转换或图像/PDF 渲染。
不。解析从不运行脚本;评估是显式的并且需要通过 JSContext 进行选择加入,模块加载由 ModuleRegistry 和 ModuleLoadPolicy 控制。