如何阅读Racket中的网页?

Ale*_*lex 7 lisp scheme racket web

我在网上找到的所有信息都是关于编写Web服务器的,但似乎很少有关于Web客户端有用的函数.理想情况下,我希望函数看起来像这样:

(website "http://www.google.com")
Run Code Online (Sandbox Code Playgroud)

并返回一个包含整个网页的字符串,但我会对任何有效的内容感到高兴.

Joh*_*nts 9

这是一个看起来像你想要的简单程序:

#lang racket

(require net/url)

(port->bytes
 (get-pure-port (string->url "http://www.google.com")))
Run Code Online (Sandbox Code Playgroud)

如果你像我一样,你可能也想把它解析成一个s表达式.Neil Van Dyke的neil/html-parsing这样做:

#lang racket

(require (planet neil/html-parsing:2:0)
         net/url)

(html->xexp
 (get-pure-port (string->url "http://www.google.com")))
Run Code Online (Sandbox Code Playgroud)

请注意,由于此程序引用的是行星包,因此首次运行此程序将下载并安装htmlprag程序包.构建文档可能需要相当长的时间.但这是一次性成本,并且再次运行程序不应该花费超过几秒钟.

  • 从端口提取所有内容时,您可能会看到“port->string”:http://docs.racket-lang.org/reference/port-lib.html?q=port-%3Estring#( def._%28%28lib._racket/port..rkt%29._port-~3estring%29%29 (2认同)