[racket] View HTML source code in racket

From: Greg Hendershott (greghendershott at gmail.com)
Date: Wed Jun 23 22:11:09 EDT 2010

How I've gotten an x-expression using solely the Racket libs:

(require net/url html xml)

(define (get-xexpr-from-url url-string)
  (call/input-url (string->url url-string)
                  get-pure-port
                  (lambda (p)
                    (xml->xexpr (car (read-html-as-xml p))))))

; example
(get-xexpr-from-url "http://www.google.com/")


To use the x-expression you'll probably want something like the
following. (Just beware I'm a Lisp/Scheme/Racket n%b and someone will
probably point out a smarter way to code this.)

(define (find-elements xexpr tag-name)
  ;; Given an x-expression return a list of all the elements starting
with tag-name.
  (if (empty? xexpr)
      '()
      (let ([x (first xexpr)])
        (if (and (list? x) (not (empty? x)))
            (if (and (symbol? (first x)) (symbol=? (first x) tag-name))
                (append (list x) ; found one!
                        (find-elements x tag-name) ; drill down
                        (find-elements (rest xexpr) tag-name)) ; iterate across
                (append (find-elements x tag-name) ; drill down
                        (find-elements (rest xexpr) tag-name))) ; iterate across
            (find-elements (rest xexpr) tag-name))))) ; iterate across

; example: get a list of all the <a> a.k.a 'a elements
(find-elements (get-xexpr-from-url "http://www.google.com/") 'a)


On Wed, Jun 23, 2010 at 2:01 PM, Insik Cho <iminsik at gmail.com> wrote:
> hi buddies,
> I want to html source of an web page in racket.
> What module and functions shall I use? and How?
> Thanks in advance!
> Joe
> _________________________________________________
>  For list-related administrative tasks:
>  http://lists.racket-lang.org/listinfo/users
>


Posted on the users mailing list.