Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Clojure以html格式从网站获取页面_Clojure - Fatal编程技术网

Clojure以html格式从网站获取页面

Clojure以html格式从网站获取页面,clojure,Clojure,从网站获取页面并在clojure中处理404重定向的最佳方式是什么 我使用了enlive,但它会自动转换页面,我不希望这样,因为我想将HTML存储在数据库中以供将来参考 (defn fetch-page [url] (html/html-resource (java.net.URL. url))) 我曾经遇到过slurp来获取原始html内容,但我不知道这是否是从外部网站检索内容的最佳方法 我遇到的第二个问题是处理404,处理它的最佳方法是什么,我的clojure程序在遇到404时不正常地

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

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

(defn fetch-page [url]
  (html/html-resource (java.net.URL. url)))
我曾经遇到过
slurp
来获取原始html内容,但我不知道这是否是从外部网站检索内容的最佳方法

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

代码:

输出:

CompilerException java.io.FileNotFoundException:


404不是重定向,它表示“未找到”。但是不管怎样,你可以像处理Clojure中的任何异常一样处理异常。。。使用
尝试
/
捕获

(try
  (slurp "http://www.google.com/doesnotexists.html")
  (catch java.io.FileNotFoundException ex
    <handle exception...>))
(试试看)
(咕噜声)http://www.google.com/doesnotexists.html")
(捕获java.io.FileNotFoundException ex
))
(try
  (slurp "http://www.google.com/doesnotexists.html")
  (catch java.io.FileNotFoundException ex
    <handle exception...>))