2016-03-15 57 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]>