0

I am a new to Clojure and enlive.

I have html like this

<SPAN CLASS="f10"><A HREF="value1" title="...." TARGET="detail">....</A></SPAN></DIV><DIV CLASS="p5"><SPAN CLASS="f10"><A HREF="value2" title="..." TARGET="detail">.....</A></SPAN>

I tried this

(html/select (fetch-url base-url) [:span.f10 [:a (html/attr? :href)]]))

but it returns this

({:tag :a,
  :attrs
  {:target "detail",
   :title
   "...",
   :href
   "value1"},
  :content ("....")}
 {:tag :a,
  :attrs
  {:target "detail",
   :title
   "....",
   :href
   "value2"},
  :content
  ("....")}

What i want is just value1 and value 2 in the output. How can i accomplish it ?

gechu
  • 217
  • 3
  • 11

2 Answers2

1

select returns the matched nodes, but you still need to extract their href attributes. To do that, you can use attr-values:

(mapcat #(html/attr-values % :href)
      (html/select (html/html-resource "sample.html") [:span.f10 (html/attr? :href)]))
alanlcode
  • 4,208
  • 3
  • 29
  • 23
0

I use this little function because the Enlive attr functions don't return the values. You are basically just walking the hash to get the value.

user=> (def data {:tag :a, :attrs {:target "detail", :title "...", :href "value1"}})
#'user/data

user=> (defn- get-attr [node attr]
       #_=>   (some-> node :attrs attr))
#'user/get-attr

user=> (get-attr data :href)
"value1"
jmargolisvt
  • 5,722
  • 4
  • 29
  • 46
  • Thank you @jmargolisvt, I tried this in my code (defn data [] (html/select (fetch-url *base-url*) [:span.f10 [:a (html/attr? :href)]])) (defn get-attr [node attr] #_=> (some-> node :attrs attr)) (defn get-api [] (get-attr data :href)) and tried executing get-api and that returns nil. Any idea? Thanks again for your help. – gechu Jan 08 '16 at 20:14
  • You have "data" defined as a function with `defn`. You need just `def`. – jmargolisvt Jan 08 '16 at 20:32