这是我html/text
直接在选择器向量中使用的示例。
(:use [net.cgrand.enlive-html :as html])
(defn fetch-url [url]
(html/html-resource (java.net.URL. url)))
(defn parse-test []
(html/select
(fetch-url "https://news.ycombinator.com/")
[:td.title :a html/text]))
调用(parse-test)
返回一个包含 Hacker News Headlines 的数据结构:
("In emergency cases a passenger was selected and thrown out of the plane. [2004]"
"“Nobody expects privacy online”: Wrong."
"The SCUMM Diary: Stories behind one of the greatest game engines ever made" ...)
凉爽的!
是否可以使用自定义函数结束选择器向量,该函数将返回文章 URL 列表。
就像是:[:td.title :a #(str "https://news.ycombinator.com/" (:href (:attrs %)))]
编辑:
这是实现这一目标的一种方法。我们可以编写自己的选择函数:
(defn select+ [coll selector+]
(map
(peek selector+)
(html/select
(fetch-url "https://news.ycombinator.com/")
(pop selector+))))
(def href
(fn [node] (:href (:attrs node))))
(defn parse-test []
(select+
(fetch-url "https://news.ycombinator.com/")
[:td.title :a href]))
(parse-test)