<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><br></div><div>Here is an old script that reads stock prices, but it still runs. -- Matthias</div><div><br></div><div><br></div><div><div>#lang racket </div><div><br></div><div>(require net/url net/uri-codec)</div><div><br></div><div>#;</div><div>(require (lib "url.ss" "net")</div><div> (lib "uri-codec.ss" "net")</div><div> (lib "list.ss")</div><div> (lib "contract.ss"))</div><div><br></div><div>(current-alist-separator-mode 'amp)</div><div><br></div><div>(define-struct no-quote (company reason))</div><div>;; no-quote = (make-no-quote String String)</div><div><br></div><div>(provide/contract</div><div> (stock-quote (string? . -> . (or/c number? string?)))</div><div> ;; stock-quote may also raise a no-quote exception:</div><div> ; (no-quote? (any? . -> . boolean?))</div><div> ; (no-quote-reason (no-quote? . -> . string?))</div><div> ) </div><div><br></div><div><br></div><div>#| ----------------------------------------------------------------------------</div><div> Stock and Fund Quotes</div><div> ---------------------</div><div> This module implements an extremely simple stock quote server. The quote </div><div> server sends a query to SOURCE and then looks for a the textual pattern </div><div> PATTERN in the resulting page. This process is brittle, and it has broken</div><div> over the years but it's easy to fix. -- If someone has a more reliable</div><div> method for obtaining stock quotes, by all means replace the implementation</div><div> of the module with something better. </div><div> |# </div><div><br></div><div>(define SOURCE "<a href="http://finance.yahoo.com/q?s=~a&d=v1">http://finance.yahoo.com/q?s=~a&d=v1</a>")</div><div><br></div><div>(define PATTERN "([0-9\\.]+)</span></span>")</div><div><br></div><div>(define (stock-quote company)</div><div> (with-handlers ([no-quote? no-quote-reason])</div><div> (let* ([PG (send-query company (form-query company))]</div><div> ; [_1 (printf "~a~n" PG)]</div><div> [LN (filter find PG)]</div><div> ; [_2 (printf "~a~n" LN)]</div><div> [QT (extract-quote company LN)])</div><div> ; (printf "~a~n" QT)</div><div> (string->number QT))))</div><div><br></div><div>;; String -> URL </div><div>;; create a URL for the stock query to the server </div><div>(define (form-query company) </div><div> (string->url (format SOURCE company))) </div><div><br></div><div>;; String URL -> (listof String)</div><div>;; contract the page at URL with a query; produce list of lines from response</div><div>;; raises no-stock-quote if it can't find the server or something goes wrong</div><div>(define (send-query company URL)</div><div> (with-handlers</div><div> ([exn:fail:network? </div><div> (lambda (e) (raise (make-no-quote company (exn-message e))))])</div><div> (call/input-url URL get-pure-port read-a-page)))</div><div><br></div><div>;; Iport -> (listof String)</div><div>;; reading a page from ip as a list of lines</div><div>(define (read-a-page ip)</div><div> (let reader ()</div><div> (let ([next (read-line ip)])</div><div> (cond</div><div> [(eof-object? next) '()]</div><div> [else (cons next (reader))]))))</div><div><br></div><div><br></div><div>;; String (union #f (list String String)) -> String</div><div>(define (extract-quote company LN)</div><div> (if (pair? LN)</div><div> ; (cadr (find (car LN)))</div><div> (cadr (find (car LN)))</div><div> (raise</div><div> (make-no-quote </div><div> company</div><div> (format "couldn't find a quote for ~a" company)))))</div><div><br></div><div>;; String -> (union #f (list String String))</div><div>;; find PATTERN in x or produce false </div><div>(define (find x) (regexp-match PATTERN x))</div><div><br></div><div><br></div><div>#|</div><div> ;; tests:</div><div> (require (lib "testing.scm" "testing"))</div><div><br></div><div> (define (good-number? x)</div><div> (test-p x number? "not a positive number"))</div><div> ; (lambda (x) (and (number? x) (> x 0)))</div><div><br></div><div> (define merck "<b>29.99</b></big></td>")</div><div> (test-p (find merck) pair? "pattern not found")</div><div> (test-p (filter find (list "" merck "" merck "")) pair? "filter find")</div><div> </div><div> (test-p (extract-quote "mrk" (filter find (list "" merck ""))) string? </div><div> "extract-quote")</div><div> (test-e (extract-quote "mrk" (filter find (list "" "xxx" ""))) no-quote </div><div> "extract-quote")</div><div><br></div><div> (printf "make sure you're connected when you run these test~n")</div><div> (good-number? (stock-quote "PRSCX")) ; a fund: T Rowe Price Science </div><div> (good-number? (stock-quote "MRK")) ; a stock: Merck </div><div> (test== (string? (stock-quote "XXXXXXX")) #t "bad stock found")</div><div> </div><div> (printf "make sure you are *not* connected when you run these test~n")</div><div> (test-p (stock-quote "MRK") string? "number found despite lack of connection")</div><div> |#</div><div><br></div></div><div><br></div><br><div><div>On Jun 12, 2013, at 3:10 PM, Frank Weytjens wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div dir="ltr"><br clear="all"><div style="">Hi Racket users,</div><div style=""><br></div><div style="">I'm trying to extract some information from a webpage.</div><div style="">First attempt is to read the complete page</div><div style="">
Later i will try to filter the StockPrice and Volume at a certain time</div><div style="">The goal is to calculate what direction the StocPrice is moving in</div><div style="">multiplied by the number of Stocks that changed owner during vast amounts of time.</div>
<div style="">Like if it was a moving/accelerating mass in fysics and you want to know it's momentum</div><div style="">The value of this program is void.</div><div style="">It's just for fun.</div><div style=""><br></div><div style="">
The first problem I encounter is that reading the webpage stops at the first curly bracket<br></div><div style=""><br></div><div style=""><div>#lang racket</div><div>(require net/url)</div><div>(define GLE (get-pure-port (string->url "<a href="http://www.tijd.be/beurzen/Societe_Generale.360017048">http://www.tijd.be/beurzen/Societe_Generale.360017048</a>")))</div>
<div>(define readPage</div><div> (lambda (ticker)</div><div> (read/recursive ticker)))</div><div>(define readMore</div><div> (lambda ()</div><div> (let ([length (pipe-content-length GLE)]) </div><div> (if (< (file-position* GLE) length) ((display (readPage GLE)) (readMore)) (print 'Finished)))))</div>
<div><br></div><div style="">(readMore)</div><div style=""><br></div><div style=""><br></div><div style="">error --> .....ascript>djConfig=. read: unexpected `}'</div><div style=""><br></div><div style=""><br></div><div style="">Sorry for the naive code, i'm just a beginner.</div>
<div style=""><br></div><div style="">Thanks in advance</div></div><div style=""> </div><div style="">Frank</div></div>
____________________<br> Racket Users list:<br> <a href="http://lists.racket-lang.org/users">http://lists.racket-lang.org/users</a><br></blockquote></div><br></body></html>