Tcl & Tk/팁 (Tip)
tdom을 이용한 html 파싱
티클러
2025. 3. 24. 10:22
아래와 같이 tdom 패키지를 이용하여 html 을 파싱 할 수 있습니다.
package require tdom
set html {<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">
<html>
<head>
</head>
<body>
<div id="m">
</div>
</body>
</html>}
set doc [ dom parse -html $html ]
set node [ $doc getElementById m]
puts [$node asList]
결과
div {id m} {}
혹은 set node [ $doc getElementById m] 대신에 아래와 같이 해도 됩니다.
puts [[$doc selectNodes "//*\[@id\]"] asList]
tdom 을 응용하면 html로 파싱할수 있다는것만 알아두셔도 좋을것 같습니다.