2017-03-01 103 views
-1

什麼是從網站獲取頁面以及在clojure中處理404重定向的最佳方式。Clojure從網站獲取html頁面

我已經使用了enlive,但它會自動轉換頁面,我不想將它存儲在數據庫中供以後參考。

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

我經歷了slurp來獲得原始的HTML內容,但我不知道這是否是檢索來自外部網站的東西是最好的方法。

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

輸出:

第二個問題我已經被處理404,是什麼來處理它的最佳方式,我Clojure的非正常程序,當它遇到一個404

代碼存在

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

回答

0

404不是重定向,它表示「未找到」。但無論如何,你可以處理異常你處理Clojure中的任何異常以同樣的方式...與try/catch

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

甜的感謝,這就是我要找的。 – Stixxxx