2

I was trying to get src attribute of img tag that has attribute itemprop set to some value.

Ok, I got the img tag and I'm able to extract inner text according to quesetion I asked here earlier today (in this case there is no text obviously), but I can't find anything that would help me return value of src attribute

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

(defn getbyitemprop 
  "Extract node content from HTML"
  [html value]
  (e/select-nodes* (e/html-snippet html)
             [(e/attr= :itemprop value) e/text-node]))

This gets me inner text of element by itemprop I pass as an argument

Vuk Stanković
  • 7,864
  • 10
  • 41
  • 65

1 Answers1

2

In this case, you want the tag, which contains the attributes and contents, so you would drop the net.cgrand.enlive-html/text-node part of the selector.

(defn getbyitemprop
  "Extract node content from HTML"
  [html value]
  (e/select-nodes* (e/html-snippet html)
                   [(e/attr= :itemprop value)]))

(getbyitemprop
  "<p itemprop=\"description\" src=\"testvalue\"> Some content I want to extract </p>"
  "description")
;=> ({:tag :p, 
;     :attrs {:src "testvalue", :itemprop "description"}, 
;     :content (" Some content I want to extract ")})

This works for dynamic strings of html, if you want more general transformations for files, or resources in general, take a look at the documentation for deftemplate and defsnippet.

Jared314
  • 5,181
  • 24
  • 30
  • this works but when I try to get value of `:src` with `(get-in (getbyitemprop html "description") [:attrs :src])` I keep getting `nil`. Same thing with `get` – Vuk Stanković Sep 21 '13 at 11:42
  • 2
    `net.cgrand.enlive-html/select-nodes*` returns a list of matches, because you can potentially have more than one match. So, you can either `(get-in (first (getbyitemprop html "description")) [:attrs :src])` or use `map` on the collection. – Jared314 Sep 21 '13 at 16:30
  • thanks. Didn't notice parentheses around curly braces. You have been great help to me – Vuk Stanković Sep 21 '13 at 19:44