0
votes

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 ?

2
Thanks @jmargolisvt , Changing html/attr? to html/attr= did not work. - gechu

2 Answers

1
votes

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)]))
0
votes

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"