1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
# 28feb11abu
# (c) Software Lab. Alexander Burger
# *ScrHost *ScrPort *ScrGate *Title *Expect *Found
# *Links *Forms *Buttons *Fields *Errors
# Scrape HTML form(s)
(de scrape (Host Port How)
(client (setq *ScrHost Host) (setq *ScrPort Port) How
(off *ScrGate *Links *Forms *Buttons *Fields *Errors)
(while
(from
"303 See Other"
"<title>"
"<base href=\"http://"
"<a href=\""
" action=\""
"<input type=\"submit\" name=\""
"<input type=\"hidden\" name=\""
"<input type=\"text\" name=\""
"<input type=\"password\" name=\""
"<select name=\""
"<option selected=\"selected\">"
"<textarea name=\""
"<span id=\""
"<div class=\"error\">"
*Expect )
(case @
("303 See Other"
(when (from "Location: http://")
(let L (split (line) ':)
(if (cdr L)
(scrape
(pack (pop 'L))
(ifn (format (car (setq L (split (car L) '/))))
80
(pop 'L)
@ )
(glue '/ L) )
(setq L (split (car L) '/))
(scrape (pack (pop 'L)) 80 (glue '/ L)) ) ) ) )
("<title>"
(setq *Title (ht:Pack (till "<"))) )
("<base href=\"http://"
(let L (split (till "\"") ':)
(if (cdr L)
(setq
*ScrHost (pack (pop 'L))
*ScrPort (format (cdr (rot (car L)))) )
(setq *ScrGate (pack (cdr (member '/ (car L))))) ) ) )
("<a href=\""
(let Url (pack *ScrGate (till "\"" T))
(from ">")
(cond
((till "<")
(queue '*Links (cons (ht:Pack @) Url)) )
((= "<img" (till " " T))
(from "alt=\"")
(queue '*Links (cons (ht:Pack (till "\"")) Url)) ) ) ) )
(" action=\""
(queue '*Forms # (action . fields)
(list (pack *ScrGate (till "\"" T))) ) )
("<input type=\"submit\" name=\""
(let Nm (till "\"" T)
(from "value=\"")
(queue '*Buttons # (label field . form)
(cons
(ht:Pack (till "\""))
(cons Nm T)
(last *Forms) ) ) ) )
("<input type=\"hidden\" name=\""
(conc (last *Forms)
(cons
(cons (till "\"" T)
(prog (from "value=\"") (ht:Pack (till "\"")))) ) ) )
(("<input type=\"text\" name=\"" "<input type=\"password\" name=\"")
(conc (last *Forms)
(cons
(queue '*Fields
(cons (till "\"" T)
(prog (from "value=\"") (ht:Pack (till "\"")))) ) ) ) )
("<select name=\""
(conc (last *Forms)
(cons
(queue '*Fields (cons (till "\"" T))) ) ) )
("<option selected=\"selected\">"
(con (last *Fields) (ht:Pack (till "<"))) )
("<textarea name=\""
(conc (last *Forms)
(cons
(queue '*Fields
(cons (till "\"" T)
(prog (from ">") (ht:Pack (till "<"))) ) ) ) ) )
("<span id=\""
(from ">")
(queue '*Fields (ht:Pack (till "<"))) )
("<div class=\"error\">"
(queue '*Errors (ht:Pack (till "<"))) )
(T (on *Found)) ) )
(or *Errors *Title) ) )
# Expect content
(de expect (*Expect . "Prg")
(let *Found NIL
(run "Prg")
(unless *Found
(quit "Content not found" *Expect) ) ) )
# Click on a link
(de click (Lbl Cnt)
(let L (cdr (target *Links Lbl Cnt))
(when (pre? "http://" L)
(setq
L (split (nth (chop L) 8) '/ ':)
*ScrHost (pack (pop 'L))
*ScrPort (ifn (format (car L)) 80 (pop 'L) @)
L (glue '/ L) ) )
(scrape *ScrHost *ScrPort L) ) )
# Press a button
(de press (Lbl Cnt)
(let B (target *Buttons Lbl Cnt)
(scrape *ScrHost *ScrPort
(cons
(caddr B)
(glue "&"
(mapcar
'((X)
(list (car X) '= (ht:Fmt (cdr X))) )
(cons (cadr B) (cdddr B)) ) ) ) ) ) )
# Retrieve a field's value
(de value (Fld Cnt)
(fin (field Fld Cnt)) )
# Set a field's value
(de enter (Fld Str Cnt)
(con (field Fld Cnt) Str) )
# Inspect current page
(de display ()
(prinl "###############")
(apply println (mapcar car *Links) 'click)
(prinl)
(apply println (mapcar car *Buttons) 'press)
(prinl)
(apply println (trim (mapcar fin *Fields)) 'value)
(prinl)
*Title )
### Utility functions ###
(de target (Lst Lbl Cnt)
(cond
((num? Lbl)
(get Lst Lbl) )
((pair Lbl) Lbl)
(T
(default Cnt 1)
(or
(find
'((L)
(and
(pre? Lbl (car L))
(=0 (dec 'Cnt)) ) )
Lst )
(quit "Target not found" Lbl) ) ) ) )
(de field (Fld Cnt)
(or
(cond
((gt0 Fld)
(get *Fields Fld) )
((lt0 Fld)
(get *Fields (+ (length *Fields) Fld 1)) )
(T (assoc Fld (cdr (get *Forms (or Cnt 1))))) )
(quit "Field not found" Fld) ) )
# vi:et:ts=3:sw=3
|