如何使用enlive获取href属性值
How to get the href attribute value using enlive
我是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)]]))
但是 returns 这个
({:tag :a,
:attrs
{:target "detail",
:title
"...",
:href
"value1"},
:content ("....")}
{:tag :a,
:attrs
{:target "detail",
:title
"....",
:href
"value2"},
:content
("....")}
我想要的只是输出中的值 1 和值 2。我怎样才能完成它?
我使用这个小函数是因为 Enlive attr
函数没有 return 值。您基本上只是通过散列来获取值。
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"
select
returns 匹配的节点,但您仍然需要提取它们的 href
属性。为此,您可以使用 attr-values
:
(mapcat #(html/attr-values % :href)
(html/select (html/html-resource "sample.html") [:span.f10 (html/attr? :href)]))
我是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)]]))
但是 returns 这个
({:tag :a,
:attrs
{:target "detail",
:title
"...",
:href
"value1"},
:content ("....")}
{:tag :a,
:attrs
{:target "detail",
:title
"....",
:href
"value2"},
:content
("....")}
我想要的只是输出中的值 1 和值 2。我怎样才能完成它?
我使用这个小函数是因为 Enlive attr
函数没有 return 值。您基本上只是通过散列来获取值。
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"
select
returns 匹配的节点,但您仍然需要提取它们的 href
属性。为此,您可以使用 attr-values
:
(mapcat #(html/attr-values % :href)
(html/select (html/html-resource "sample.html") [:span.f10 (html/attr? :href)]))