我正在使用activize 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"
))
如何获得所需的结果
看看zipmap
(zipmap (read-specific-other each-rows) (read-specific-content each-rows))
如果你真的想让关键字成为关键字,那么就使用keyword
函数;但我建议把琴弦作为钥匙。
也可以考虑使用into for
模式:
(into {}
(for [[{:keys [attrs]} {:keys [content]}] rows]
[content attrs]))