如何在Racket中阅读网页? [英] How do I read a web page in Racket?

查看:49
本文介绍了如何在Racket中阅读网页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

All of the information I can find online is about writing web servers, but there seems to be very little about functions useful for web clients. Ideally, I would like the function to look something like this:

(website "http://www.google.com")

并返回一个包含整个网页的字符串,但是我对任何有效的方法都很满意.

And return a string containing the entire web page, but I would be happy with anything that works.

推荐答案

这是一个简单的程序,看起来像它可以满足您的要求:

Here's a simple program that looks like it does what you want:

#lang racket

(require net/url)

(port->bytes
 (get-pure-port (string->url "http://www.google.com")))

如果您像我一样,可能还想将其解析为s表达式.尼尔·范戴克(Neil Van Dyke)的 neil/html-parsing 做到了:

If you're like me, you probably also want to parse it into an s-expression. Neil Van Dyke's neil/html-parsing does this:

#lang racket

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

(html->xexp
 (get-pure-port (string->url "http://www.google.com")))

请注意,由于该程序引用了Planet软件包,因此首次运行此程序将下载并安装htmlprag软件包.构建文档可能需要一段时间.不过,这是一次性的费用,并且再次运行该程序所花费的时间不会超过几秒钟.

Note that since this program refers to a planet package, running this program for the first time will download and install the htmlprag package. Building the documentation could take quite a while. That's an one-time cost, though, and running the program again shouldn't take more than a few seconds.

这篇关于如何在Racket中阅读网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆