web client in Python Q

Jerry Hill malaclypse2 at gmail.com
Wed May 7 21:14:52 EDT 2008


On Wed, May 7, 2008 at 8:22 PM, Jive Dadson <notontheweb at noisp.com> wrote:
> Hey folks.  I know approximately zero about web clients.  There's a simple
> task I want to do. (I think it's probably simple.) And I figure a Python
> script ought to be just the ticket.
>
>  Various web services for daily stock ticker info.  For example,
>
>  http://finance.google.com/finance/historical?q=NYSE:APC&output=csv
>
>  and
>
> http://download.finance.yahoo.com/d/quotes.csv?s=APC&f=sl1d1t1c1ohgv&e=.csv

That should be a pretty straightforward script.  Take a look at this,
and see if it helps:

>>> import urllib
>>> page = urllib.urlopen('http://finance.google.com/finance/historical?q=NYSE:APC&output=csv')
>>> data = page.readlines()
>>> data[0]
'Date,Open,High,Low,Close,Volume\n'
>>> data[1]
'7-May-08,76.13,77.94,75.60,76.68,9501300\n'
>>> data[1].strip().split(',')
['7-May-08', '76.13', '77.94', '75.60', '76.68', '9501300']

>>> page = urllib.urlopen('http://download.finance.yahoo.com/d/quotes.csv?s=APC&f=sl1d1t1c1ohgv&e=.csv')
>>> data = page.readlines()
>>> data[0]
'"APC",76.68,"5/7/2008","4:01pm",+2.15,76.14,77.94,75.60,9501342\n'
>>> data[1]

Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    data[1]
IndexError: list index out of range
>>>

The urllib documentation is here: http://docs.python.org/lib/module-urllib.html
Both of those formats look pretty straightforward to parse, but if
you're dealing with more complicated CSV files, take a look at the csv
module.

-- 
Jerry



More information about the Python-list mailing list