[plt-scheme] Beginner-friendly Net access functions?

From: Jean-Paul.ROY at unice.fr (Jean-Paul.ROY at unice.fr)
Date: Wed Jun 21 03:31:08 EDT 2006

I introduce my own students to regular expressions and Web  
programming very slightly, through this kind of example, finding  
information on a Web page, which has always the same structure and is  
regularly updated. Nice for the Cookbook ?

;;; value of the NASDAQ in (almost) real time:

(define (nasdaq)
   (define-values (p-in p-out) (tcp-connect "www.boursorama.com"  
80))       ; or let-values...
   (fprintf p-out "GET http://www.boursorama.com/international/ 
international.phtml HTTP/1.0\n\n")
   ;;; searching the magic line, with the word COMPOSITE (you have to  
read the source page for that)
   (letrec ((iter (lambda (str)
                    (cond ((eof-object? str) (error "Bug : Word  
COMPOSITE not found ?????"))
                          ((regexp-match "COMPOSITE" str) str)
                          (else (iter (read-line p-in)))))))
     ;;; the information is on the following line :
     (let* ((good-line (iter (read-line p-in)))
            (result (car (regexp-match "[0-9.]+[%]" good-line))))
       (close-input-port p-in)
       (close-output-port p-out)
       (string->number (regexp-replace "%" result "")))))     ; skip  
% and convert...

Hope it helps,

     -jpr


Posted on the users mailing list.