CLISP open-http示例

时间:2016-03-15 05:31:36

标签: http lisp common-lisp clisp

我正在尝试使用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的示例。谢谢!

1 个答案:

答案 0 :(得分: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我无法使其工作,因为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

如果&#34; https&#34;被&#34; http&#34;取代,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]>