我正在使用enlive clojure来解析HTML。我的解析器看起来像;
(def each-rows
(for [tr crawl-page
:let [row (html/select tr [:td (attr= :class "bl_12")])]
:when (seq row)]
row))
提取结果如下;
{:tag :a,
:attrs
{:class "bl_12",
:href
"url1"},
:content ("Chapter 1")}
{:tag :a,
:attrs
{:class "bl_12",
:href
"url2"},
:content ("Chapter 2")}
{:tag :a,
:attrs
{:class "bl_12",
:href
"url3"},
:content ("Chapter 3")}
现在我的目标是获得这样的字典;
{:Chapter_1 "url1"
:Chapter_2 "url2"
:Chapter_3 "url3"}
我设法编写了一个只提取href或仅提取内容但不能将其作为地图的方法
(defn read-specific-other [x]
(map (comp second :attrs) x))
输出:[:href "url1"]
(defn read-specific-content [x]
(map (comp first ::content) x))
(映射每行读取特定内容)
输出:
(("Chapter 1"
"Chapter 2"
"Chapter 3"
))
如何获得所需的结果
答案 0 :(得分:2)
查看zipmap
(zipmap (read-specific-other each-rows) (read-specific-content each-rows))
如果您确实希望密钥是关键字,请使用keyword
功能;但我建议把字符串作为键。
另请考虑使用into for
模式:
(into {}
(for [[{:keys [attrs]} {:keys [content]}] rows]
[content attrs]))