Haskell Language
태그가 달린 렌즈와 렌즈로 HTML 구문 분석하기
수색…
div의 텍스트 내용을 특정 ID로 추출합니다.
Taggy-lens를 사용하면 렌즈를 사용하여 HTML 문서를 구문 분석하고 검사 할 수 있습니다.
#!/usr/bin/env stack
-- stack --resolver lts-7.0 --install-ghc runghc --package text --package lens --package taggy-lens
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.Text.Lazy as TL
import qualified Data.Text.IO as T
import Text.Taggy.Lens
import Control.Lens
someHtml :: TL.Text
someHtml =
"\
\<!doctype html><html><body>\
\<div>first div</div>\
\<div id=\"thediv\">second div</div>\
\<div id=\"not-thediv\">third div</div>"
main :: IO ()
main = do
T.putStrLn
(someHtml ^. html . allAttributed (ix "id" . only "thediv") . contents)
트리에서 요소 필터링하기
id="article"
div를 찾고 모든 내부 스크립트 태그를 제거합니다.
#!/usr/bin/env stack
-- stack --resolver lts-7.1 --install-ghc runghc --package text --package lens --package taggy-lens --package string-class --package classy-prelude
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
import ClassyPrelude
import Control.Lens hiding (children, element)
import Data.String.Class (toText, fromText, toString)
import Data.Text (Text)
import Text.Taggy.Lens
import qualified Text.Taggy.Lens as Taggy
import qualified Text.Taggy.Renderer as Renderer
somehtmlSmall :: Text
somehtmlSmall =
"<!doctype html><html><body>\
\<div id=\"article\"><div>first</div><div>second</div><script>this should be removed</script><div>third</div></div>\
\</body></html>"
renderWithoutScriptTag :: Text
renderWithoutScriptTag =
let mArticle :: Maybe Taggy.Element
mArticle =
(fromText somehtmlSmall) ^? html .
allAttributed (ix "id" . only "article")
mArticleFiltered =
fmap
(transform
(children %~
filter (\n -> n ^? element . name /= Just "script")))
mArticle
in maybe "" (toText . Renderer.render) mArticleFiltered
main :: IO ()
main = print renderWithoutScriptTag
-- outputs:
-- "<div id=\"article\"><div>first</div><div>second</div><div>third</div></div>"
@ duplode의 SO 대답을 기반으로 한 기부
Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow