Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enlive - Extract content of tag if attribute has designated value

Tags:

clojure

enlive

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>

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.

like image 509
Vuk Stanković Avatar asked Sep 20 '13 06:09

Vuk Stanković


1 Answers

To get the text content of any node with the specific attribute, the selector would look something like the following:

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

[(e/attr= :itemprop "description") e/text-node]

If the contents contain a mix of text and tags, and you wanted to keep both of them, you should use net.cgrand.enlive-html/any-node instead of net.cgrand.enlive-html/text-node.

You can test it with the following:

(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 ")
like image 123
Jared314 Avatar answered Sep 20 '22 17:09

Jared314