首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CLISP open-http示例

CLISP open-http示例
EN

Stack Overflow用户
提问于 2016-03-15 13:31:36
回答 1查看 196关注 0票数 0

我正在尝试用CLISP阅读一系列网页,如果它们存在的话,但我不明白open-http是如何跳过不存在的网页的。我有以下几点:

代码语言:javascript
复制
(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- to not-exist是如何工作的和/或提供如何使用open-http的例子。谢谢!

EN

回答 1

Stack Overflow用户

发布于 2016-03-15 22:39:57

这对我来说是可行的:

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

输出:

代码语言:javascript
复制
;; connecting to "http://stackoverflow.com/questions/234242424242"...connected...HTTP/1.1 404 Not Found
;; HTML source of Page not found
Stream: NIL
NIL

获取连接会有延迟,但它可以正常工作。

如果页面确实存在:

代码语言:javascript
复制
[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:

代码语言:javascript
复制
[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不会构造正确的地址:

代码语言:javascript
复制
[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]> 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36003343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档