2016-03-15 54 views
0

我正在嘗試使用CLISP讀取一系列網頁(如果存在),但我不明白open-http如何工作以跳過不存在的網頁。 我有以下幾點:CLISP open-http示例

(dolist (word '(a b c)) 
    (with-open-stream (stream (ext:open-http 
           (format nil 
             "https://en.wikipedia.org/wiki/~a.html" 
             word) 
           :if-does-not-exist nil)) 
    (when stream 
     (print word)))) 

我想簡單地跳過一個網頁,如果它不存在,但似乎CLISP掛起,並返回一個「無效參數」的錯誤。 任何人都可以解釋這個參數:if-does-not-exist的工作原理和/或提供如何使用open-http的例子。謝謝!

+0

我想你在這個過程中會遇到其他問題:幾乎所有的網站現在都使用HTTPS,CLISP的OPEN-HTTP不支持HTTPS。另外,就我所見,維基百科的文章並不放在* .html文件中,其他文件的路徑更復雜。 – mobiuseng

+0

相關:[是否有Wikipedia API?](http://stackoverflow.com/q/627594/124319)。另外,請看[Drakma](http://weitz.de/drakma/) – coredump

+0

感謝迄今爲止的回覆,但請關注以下問題:如何防止在頁面未打開時掛起http存在,無論其網址。一個例子就足夠了。 – Leo

回答

1

它的工作對我來說:

(with-open-stream (stream (ext:open-http 
          "http://stackoverflow.com/questions/234242424242" 
          :if-does-not-exist nil)) 
(format t "~&Stream: ~A~%" stream)) 

輸出:

;; connecting to "http://stackoverflow.com/questions/234242424242"...connected...HTTP/1.1 404 Not Found 
;; HTML source of Page not found 
Stream: NIL 
NIL 

有一個延遲,以獲得連接,但它的工作原理。

如果頁面確實存在:

[7]> (with-open-stream (stream (ext:open-http 
           "http://stackoverflow.com/questions/36003343/clisp-open-http-example" 
           :if-does-not-exist nil)) 
     (format t "~&Stream: ~A~%" stream)) 
;; connecting to "http://stackoverflow.com/questions/36003343/clisp-open-http-example"...connected...HTTP/1.1 200 OK 
Stream: #<IO INPUT-BUFFERED SOCKET-STREAM CHARACTER stackoverflow.com:80> 
NIL 

維基百科我不能讓它工作,因爲Wikipedia.org它重定向到HTTPS和EXT:OPEN-HTTP也不能直接處理HTTPS,也不能處理重定向:

這裏,如果HTTPS是直接使用:

[10]> (with-open-stream (stream (ext:open-http 
           "https://en.wikipedia.org/wiki/Common_Lisp" 
           :if-does-not-exist nil)) 
     (format t "~&Stream: ~A~%" stream)) 

*** - OPEN-HTTP: "https://en.wikipedia.org/wiki/Common_Lisp" is not an HTTP URL 
The following restarts are available: 
ABORT   :R1  Abort main loop 
Break 1 [11]> :r1 

如果 「https」 開頭是 「HTTP」 取代,CLISP不構建一個合適的地址:

[12]> (with-open-stream (stream (ext:open-http 
           "http://en.wikipedia.org/wiki/Common_Lisp" 
           :if-does-not-exist nil)) 
     (format t "~&Stream: ~A~%" stream)) 
;; connecting to "http://en.wikipedia.org/wiki/Common_Lisp"...connected...HTTP/1.1 301 TLS Redirect --> "https://en.wikipedia.org/wiki/Common_Lisp" 
;; connecting to "http://en.wikipedia.orghttps://en.wikipedia.org/wiki/Common_Lisp"... 
*** - PARSE-INTEGER: substring "" does not have integer syntax at position 0 
The following restarts are available: 
ABORT   :R1  Abort main loop 
Break 1 [13]>