通过enlive,您如何根据其内容获取链接的标记?

时间:2013-03-28 04:32:58

标签: clojure enlive

我有一些看起来像这样的HTML:

<html>
<body>
<a href="www.google.com">text</a>
</body>
</html>

如何通过使用“查找包含文本内容的任何链接”的选择器来获取a?我已经了解了text-pred,但这只是返回文本,而不是带有文本的标记。

1 个答案:

答案 0 :(得分:6)

I found this answer on the Enlive forum。为方便起见,我会内联:

问题

I'm trying to extract the :href attribute from a link which has the
anchor text "Next".  So far I have the following selector worked up.

(html/select tree [:div#nav :span.b :a])

<div id="nav">
<span class="b"><a href="...">Back</a></span>
<span><a href="...">1</a></span>
<span><a href="...">2</a></span>
<span><a href="...">3</a></span>
<span class="b"><a href="...">Next</a></span>
</div>

The problem is that this gives several results (both "Back" and
"Next").  How can I filter this by the text above so I just get the
element I want?  I'd prefer to keep the logic in the css selector
instead of looping through the results if possible...

答案

You have different options:
  [:div#nav :span.b [:a (html/has [(html/re-pred #"Next")])]]
  [:div#nav :span.b [:a (html/has [(html/text-pred #(= % "Next"))])]]
but the simplest is:
  [:div#nav :span.b [:a (html/pred #(= (html/text %) "Next"))]]
and you can make it clearre by rolling your own predicate:
  (defn text= [s] (html/pred #(= s (html/text %))))
  [:div#nav :span.b [:a (text= "Next")]]

#'text works like innerText in browsers so this selector would match <a href='#'><b>Ne<!-- boo -->xt</b></a> too/