Enlive - 提取具有指定属性值的标签内容

9
我正在尝试使用Clojure和Enlive提取HTML标签

中的内容,条件是其中一个属性具有我指定的值。类似于这样:

<p itemprop="description"> Some content I want to extract </p>

如果 itemprop="description",我想要提取 一些我想提取的内容

我对Clojure非常陌生,需要帮助。

1个回答

10

为了获取带有特定属性的任何节点的文本内容,选择器应该像以下示例一样:

(require '[net.cgrand.enlive-html :as e])

[(e/attr= :itemprop "description") e/text-node]
如果内容包含文本和标签混合,并且您希望保留它们两者,您应该使用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 ")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接