【发布时间】:2015-01-23 21:19:59
【问题描述】:
保存和重新加载enlive 的html-resource 输出的适当json 方式是什么。
以下过程不保留数据结构(注意我要求 json/read-str 将键映射到符号):
(require net.cgrand.enlive-html :as html)
(require clojure.data.json :as json)
(def craig-home
(html/html-resource (java.net.URL. "http://www.craigslist.org/about/sites")))
(spit "./data/test_json_flow.json" (json/write-str craig-home))
(def craig-reloaded
(json/read-str (slurp "./data/test_json_flow.json") :key-fn keyword))
(defn count-nodes [page] (count (html/select page [:div.box :h4])))
(println (count-nodes craig-home)) ;; => 140
(println (count-nodes craig-reloaded)) ;; => 0
谢谢。
更新
为了解决 Mark Fischer 的评论,我发布了一个不同的代码,地址为 html/select 而不是 html/html-resource
(def craig-home
(html/html-resource (java.net.URL. "http://www.craigslist.org/about/sites")))
(def craig-boxes (html/select craig-home [:div.box]))
(count (html/select craig-boxes [:h4])) ;; => 140
(spit "./data/test_json_flow.json" (json/write-str craig-boxes))
(def craig-boxes-reloaded
(json/read-str (slurp "./data/test_json_flow.json") :key-fn keyword))
(count (html/select craig-boxes-reloaded [:h4])) ;; => 0
【问题讨论】:
-
这似乎不对,您的 craig-reloaded 和 craig-home 返回不同的类型,重新加载的版本没有返回 count-nodes 方法可以使用的 html,是吗?跨度>
-
@mark 你是对的。我搞砸了我的例子。实际上我的问题是关于 jsonizing
html/select结果并进一步解析它们。我更新了例子