I am trying use Clojure and Enlive to extract content of p html tag under condition that one of attributes has values I designated. Something like this
<p itemprop="description"> Some content I want to extract </p>
Run Code Online (Sandbox Code Playgroud)
So I want to get Some content I want to extract if itemprop="description".
I am very new to Clojure so help would be great.
Jar*_*314 10
要获取具有特定属性的任何节点的文本内容,选择器将如下所示:
(require '[net.cgrand.enlive-html :as e])
[(e/attr= :itemprop "description") e/text-node]
Run Code Online (Sandbox Code Playgroud)
如果内容包含文本和标签的混合,并且您希望保留它们,则应使用net.cgrand.enlive-html/any-node而不是net.cgrand.enlive-html/text-node.
您可以使用以下方法对其进行测试:
(require '[net.cgrand.enlive-html :as e])
(def data "<p itemprop=\"description\"> Some content I want to extract </p>")
(e/select-nodes* (e/html-snippet data)
[(e/attr= :itemprop "description") e/text-node])
;=> (" Some content I want to extract ")
Run Code Online (Sandbox Code Playgroud)