[racket] Just how to get and print a web page

From: Sam Tobin-Hochstadt (samth at ccs.neu.edu)
Date: Sat Oct 29 07:15:05 EDT 2011

On Sat, Oct 29, 2011 at 4:27 AM, MingQian Zhang <zmingqian at qq.com> wrote:
> Hi all:
> I am a rookie to racket who used to write some ruby scripts.
> Now I meet some problem about how to download a URL and print it or deal the
> result as a String.
> I write like this:
> #lang racket
> (require net/url)
> (define (get url)
> (get-impure-port (string->url url)))
> Then run:
>> (read (get "http://www.google.com"))
> 'HTTP/1.0
>>
> Only output part of the total response.
> I am confused about this.

The problem here is that `read' a specialized procedure for reading a
certain kind of data, called s-expressions.  You just want to read
whatever Google sends you over the port.

> How can I read and print all the response page?

Here's a fixed version:

#lang racket
(require net/url)
(define (get url)
  (get-impure-port (string->url url)))
(port->lines (get "http://www.google.com"))

> Can anyone explain this ?
> Thanks very much!
> By the way, as a stater on this language ,I found the racket docs very
> useful.
> But sometime can't understand the reference and don't know how people use
> these functions.
> Are there any source code using racket to read?

Have you looked at the "Guide" documentation?  It's a lot easier for
new Racket programmers than the Reference?
http://docs.racket-lang.org/guide/index.html
-- 
sam th
samth at ccs.neu.edu


Posted on the users mailing list.