0

我是 Clojure 的新手并且很活跃。

我有这样的html

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

我试过这个

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

但它返回这个

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

我想要的只是输出中的 value1 和 value 2。我怎样才能完成它?

4

2 回答 2

1

select返回匹配的节点,但您仍然需要提取它们的href属性。为此,您可以使用attr-values

(mapcat #(html/attr-values % :href)
      (html/select (html/html-resource "sample.html") [:span.f10 (html/attr? :href)]))
于 2016-01-08T19:53:53.550 回答
0

我使用这个小函数是因为 Enliveattr函数不返回值。您基本上只是通过散列来获取值。

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"
于 2016-01-08T19:46:39.757 回答