我遵循了 enlive 教程,必须说我对 Enlive 解析网络的能力印象深刻。现在,我进一步查看了此处可用的 scrape3.clj:https ://github.com/swannodette/enlive-tutorial/blob/master/src/tutorial/scrape3.clj
Swannodette 在设计这个例子方面做得很好,但我觉得我们可以让它变得更枯燥一些。
我的问题:我会重写此提取函数以使其更干燥:
(defn extract [node]
(let [headline (first (html/select [node] *headline-selector*))
byline (first (html/select [node] *byline-selector*))
summary (first (html/select [node] *summary-selector*))
result (map html/text [headline byline summary])]
(zipmap [:headline :byline :summary] (map #(re-gsub #"\n" "" %) result))))
如果您对程序的其他元素有其他想法,请随时分享!
编辑:我到处玩,想出了:
(defn extract [node]
(let [s [*headline-selector* *byline-selector* *summary-selector*]
selected (map #(html/text (first (html/select [node] %))) s)
cleaned (map #(re-gsub #"\n" "" %) selected)]
(zipmap [:headline :byline :summary] cleaned)))