gpt4 book ai didi

clojure - 使用 enlive,您如何根据链接的内容获取链接的标签?

转载 作者:行者123 更新时间:2023-12-04 18:45:13 26 4
gpt4 key购买 nike

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

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

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

最佳答案

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/

关于clojure - 使用 enlive,您如何根据链接的内容获取链接的标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15674335/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com