【问题标题】:Clojure getting a page from a website as htmlClojure 从网站获取页面为 html
【发布时间】:2017-03-01 07:17:37
【问题描述】:

从网站获取页面并在 clojure 中处理 404 重定向的最佳方式是什么。

我使用了 enlive,但它会自动转换页面,我不希望这样做,因为我想将 HTML 存储在数据库中以供将来参考。

(defn fetch-page [url]
  (html/html-resource (java.net.URL. url)))

我遇到过slurp 来获取原始 html 内容,但我不知道这是否是从外部网站检索内容的最佳方法。

我遇到的第二个问题是处理 404,最好的处理方法是什么,我的 clojure 程序在遇到 404 时不正常地存在。

代码:

(println (slurp "http://www.google.com/doesnotexists.html"))

输出:

CompilerException java.io.FileNotFoundException: http://www.google.com/doesnotexists.html

【问题讨论】:

    标签: clojure


    【解决方案1】:

    404 不是重定向,它表示“未找到”。但无论如何,您可以像处理 Clojure 中的任何异常一样处理异常......使用try/catch

    (try
      (slurp "http://www.google.com/doesnotexists.html")
      (catch java.io.FileNotFoundException ex
        <handle exception...>))
    

    【讨论】:

      猜你喜欢
      • 2016-07-13
      • 1970-01-01
      • 1970-01-01
      • 2014-08-16
      • 1970-01-01
      • 2016-10-13
      • 2020-10-06
      • 2018-03-20
      • 1970-01-01
      相关资源
      最近更新 更多