Pendahuluan
Lapisan DOM adalah inti dari Aspose.HTML FOSS untuk Python. Ia memodelkan halaman HTML sebagaimana standar — sebagai pohon node Document, Element, Text, Comment, dan DocumentFragment — dan memberikan Anda operasi untuk membangun, memeriksa, serta mengatur ulang pohon tersebut dari kode Python, tanpa melibatkan peramban.
Postingan ini membahas fitur utama DOM: membuat node, mengelola atribut, memindahkan subpohon dengan operasi Node, parsing fragmen, dan mencari kembali elemen setelah pohon dibangun. Semua yang ditampilkan di sini disertakan dalam paket aspose-html-foss di PyPI, berlisensi MIT, dan berjalan dalam Python murni pada versi 3.10 atau lebih baru.
Fitur Utama
Buat Dokumen dan Elemen
Document adalah pabrik node: create_element() untuk elemen, create_text_node() untuk teks, create_comment() untuk komentar, dan create_document_fragment() untuk kontainer ringan. Node baru terhubung ke pohon dengan append_child():
from aspose_html.dom import Document
doc = Document()
el = doc.create_element("div")
doc.append_child(el)
Kelola Atribut
Atribut melakukan round-trip melalui Element.set_attribute(), get_attribute(), has_attribute(), dan remove_attribute(). Kelas Attr memodelkan node atribut individual ketika Anda membutuhkannya sebagai objek bukan string:
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)
Operasi Pohon pada Node
Setiap node mewarisi toolkit pohon standar dari Node: insert_before(), remove_child(), replace_child(), clone_node(), dan contains() untuk perubahan dan pemeriksaan struktur, plus child_nodes, parent_node, dan first_child untuk penelusuran, serta text_content untuk membaca atau menetapkan teks dari sebuah subtree. get_root_node() dan has_child_nodes() menjawab pertanyaan struktural umum secara langsung.
Mengurai Fragmen dan Markup Bersebelahan
HTMLDocument.parse() menangani halaman lengkap dan HTMLDocument.parse_fragment() menangani markup parsial. Untuk penyisipan secara presisi relatif terhadap elemen yang ada, Element menyediakan insert_adjacent_html(), insert_adjacent_text(), dan insert_adjacent_element() — penyisipan empat-posisi standar API. Document.import_node() dan adopt_node() memindahkan node antar dokumen.
Cari dan Baca Kembali
Document.get_element_by_id() mengambil sebuah elemen berdasarkan atribut id. Digabungkan dengan akses computed-style dari lapisan CSSOM, Anda dapat memverifikasi apa yang terkandung dalam sebuah subtree dan bagaimana ia akan di-styling:
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"))
Panduan Cepat
Instal dari PyPI:
asposefoss/html is not yet published — build from source until it ships. See the project README for build instructions.Bangun sebuah pohon kecil, beri tag dengan atribut, dan konfirmasi strukturnya:
from aspose_html.dom import Document
doc = Document()
container = doc.create_element("div")
container.set_attribute("id", "content")
doc.append_child(container)
child = doc.create_element("p")
container.append_child(child)
print(container.has_child_nodes())
print(doc.get_element_by_id("content").get_attribute("id"))
Sumber Terbuka & Lisensi
Aspose.HTML FOSS untuk Python berlisensi MIT — gratis untuk penggunaan komersial, modifikasi, dan redistribusi. Kode berada di GitHub, dan rilis dipublikasikan ke PyPI sebagai aspose-html-foss.