はじめに
Aspose.HTML FOSS for Python は、Python で HTML ドキュメントを扱うための新しいオープンソースライブラリです。マークアップを標準ベースのドキュメントオブジェクトモデルにパースし、プログラムから要素ツリーを構築・変更でき、CSS(特異性、インライン宣言、継承を含む)を解決して、任意の要素から取得できる計算済みスタイルにします。
このライブラリは、ブラウザが利用できない、または不要なバックエンドおよびツールチェーンのシナリオを対象としています:保存された HTML をクリーンアップするサービス、ページフラグメントを再構築するパイプライン、生成されたマークアップを検証するテストスイート。PyPI から aspose-html-foss としてインストールでき、MIT ライセンスで、純粋な Python for 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-style URL 解析と検証を処理し、URLSearchParams(append()、get()、has()、delete())でクエリ文字列を扱います。生バイトの場合、detect_encoding() はエンコーディングを sniff します — byte-order marks included — そしてデコードされたテキストと信頼度レベルを返し、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 として公開されています。