Search code examples
clojureenlive

Get attribute of html tag - Enlive


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


Solution

  • 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.