소개
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 이름으로 게시됩니다.