Aspose.HTML FOSS for Python เป็นไลบรารีฟรีและโอเพ่นซอร์สสำหรับทำงานกับเอกสาร HTML ในแอปพลิเคชัน Python ติดตั้งด้วยคำสั่ง 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, URL และ URLSearchParams แบบ WHATWG, และตัวตรวจจับสตรีมไบต์ detect_encoding() — พร้อมใช้งานเมื่อคุณต้องการควบคุมโดยตรง
Aspose.HTML FOSS มีใบอนุญาต MIT และถูกพัฒนาใน Python แท้ โดยต้องการ Python 3.10 หรือใหม่กว่า มันทำงานเหมือนกันบน Windows, Linux, และ macOS — รวมถึง CI runners, Docker containers, และสภาพแวดล้อมแบบ serverless สำหรับกลุ่มผลิตภัณฑ์ระดับองค์กร ดู 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_contentserialise()CSSStyleSheet.replace_sync() และแนบมันผ่าน Document.attach_style_sheet()style ของอิลิเมนต์ด้วย CSSStyleDeclaration.set_property()Element.get_computed_style()CSSStyleDeclaration.get_property_value()Tokenizer.tokenize() หรือ Tokenizer.tokenize_fragment()TreeBuilder.run()parse_html()URL.parse() และ URL.can_parse()URLSearchParams — append(), get(), has(), delete()detect_encoding()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 — การใช้เชิงพาณิชย์ การแก้ไข และการแจกจ่ายทั้งหมดได้รับอนุญาต; ข้อกำหนดเดียวคือการคงการแจ้งลิขสิทธิ์ไว้กับสำเนาที่สำคัญ.
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