【问题标题】:Clojure Enlive: How to convert enlive nested map data back into HTML?Clojure Enlive:如何将 enlive 嵌套地图数据转换回 HTML?
【发布时间】:2014-02-25 19:56:52
【问题描述】:

我正在为一个网站编写一个爬虫,目标是创建一个重新格式化的网站版本。作为抓取的一部分,我深入研究了一些可能包含 html 格式的 cmets,因此我们有:

{... :content ("而且,用小声说,\"这是" {:tag :em, :attrs nil, :content ("common")} "?\"")}

问题是:我可以获取这个 :content 值的内容并使用内置的 enlive 函数将它们转换为 HTML,如下所示:

常见吗?

我可以看到如何编写一些东西来处理这些标签,但我对自制任何东西都非常犹豫,因为我可能会错过边缘情况。

【问题讨论】:

    标签: html clojure screen-scraping enlive


    【解决方案1】:

    据我所知不是内置的,而且似乎过于具体,无法内置。我的解决方案:

    (require '[net.cgrand.enlive-html :as html])
    
    (def my-node '{:tag :p, 
                   :content ("And, in a lower voice, \"Is this" 
                            {:tag :em, :attrs nil, :content ("common")} "?\"")})
    
    ;; for escaped string:
    (apply str (html/emit* (:content my-node)))
    => "And, in a lower voice, \"Is this<em>common</em>?\""
    
    ;; print in human readable form
    (print (apply str (html/emit* (:content my-node))))
    => And, in a lower voice, "Is this<em>common</em>?"
       nil
    

    【讨论】:

      猜你喜欢
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 2016-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多