Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

stock quotes

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


donlingerfelt at cox

Sep 13, 2006, 6:56 PM

Post #1 of 8 (1874 views)
Permalink
stock quotes

I would like to download stock quotes from the web, store them, do
calculations and sort the results. However I am fairly new and don't have a
clue how to parse the results of a web page download. I can get to the
site, but do not know how to request the certain data need. Does anyone
know how to do this? I would really appreciate it. Thanks.


--
http://mail.python.org/mailman/listinfo/python-list


george.sakkis at gmail

Sep 13, 2006, 7:26 PM

Post #2 of 8 (1835 views)
Permalink
Re: stock quotes [In reply to]

Donlingerfelt wrote:
> I would like to download stock quotes from the web, store them, do
> calculations and sort the results. However I am fairly new and don't have a
> clue how to parse the results of a web page download. I can get to the
> site, but do not know how to request the certain data need. Does anyone
> know how to do this? I would really appreciate it. Thanks.

This will get you started:
http://www.crummy.com/software/BeautifulSoup/

George

--
http://mail.python.org/mailman/listinfo/python-list


larry.bates at websafe

Sep 13, 2006, 7:33 PM

Post #3 of 8 (1842 views)
Permalink
Re: stock quotes [In reply to]

Donlingerfelt wrote:
> I would like to download stock quotes from the web, store them, do
> calculations and sort the results. However I am fairly new and don't have a
> clue how to parse the results of a web page download. I can get to the
> site, but do not know how to request the certain data need. Does anyone
> know how to do this? I would really appreciate it. Thanks.
>
>
It depends:

If the HTML on the page(s) you want to process is clean and well formed it
can be pretty easy. Download elementree from:

http://effbot.org/zone/element-index.htm

This will be included in Python 2.5 standard library, but for 2.4 and
older you need to download it.

If the HTML on the page(s) is not clean or well formed. Parsing the
HTML can be "messey". There is a module called BeautifulSoup that is
pretty good at this. You will need to get it from:

http://www.crummy.com/software/BeautifulSoup/#Download

In addition you will want to use the standard library urllib module
to connect to and read the information you want to parse.

Start with urllib and get it to read your HTML first, then use either
elementtree or beautifulsoup to parse it and extract the data you
want to get.

Note: If the web page gets changed, it can break your program.

Hope information helps.

-Larry
--
http://mail.python.org/mailman/listinfo/python-list


skip at pobox

Sep 13, 2006, 8:36 PM

Post #4 of 8 (1845 views)
Permalink
Re: stock quotes [In reply to]

don> I would like to download stock quotes from the web, store them, do
don> calculations and sort the results. However I am fairly new and
don> don't have a clue how to parse the results of a web page download.
don> I can get to the site, but do not know how to request the certain
don> data need. Does anyone know how to do this? I would really
don> appreciate it.

Before you launch into a screen scraping exercise, you might want to check
around to see if there are any web services apis which already provide stock
quotes. This might be a good place to start:

http://www.programmableweb.com/

Skip
--
http://mail.python.org/mailman/listinfo/python-list


belred at gmail

Sep 13, 2006, 9:50 PM

Post #5 of 8 (1842 views)
Permalink
Re: stock quotes [In reply to]

Donlingerfelt wrote:
> I would like to download stock quotes from the web, store them, do
> calculations and sort the results. However I am fairly new and don't have a
> clue how to parse the results of a web page download. I can get to the
> site, but do not know how to request the certain data need. Does anyone
> know how to do this? I would really appreciate it. Thanks.
>
>

i recently wrote a moinmoin macro which i believe does exactly what you want.
even though you aren't writing a moinmoin macro, all the stuff you need is here.

usage: [[stock(amzn,goog,yhoo)]]

note that the string 'amzn,goog,yahoo' is passed in as the symbols variable and
is placed as-is onto the url. you will receive one .csv file from yahoo with
*all* the ticker info for all symbols you requested... very cool :) then for
each row in the csv file, i pull out each column (data) and set a red or green
color based on whether the stock is up or down for the day as well as providing
a link to the yahoo finance site (finance.google.com in my latest version) when
that symbol is clicked. and finally return an html table with the data.

i hope this helps you. i apologize in advance if this code doesn't come through
the newsgroup formatted properly.



import urllib
import csv
def execute(macro, symbols):
color = 'black'
try:
reader =
csv.reader(urllib.urlopen('http://finance.yahoo.com/d/quotes.csv?s=%s&f=sl1d1t1c1ohgv&e=.csv'
% symbols))
data = []
for symbol, trade, date, time, change, opened, hi, low, volume in reader:
num = float(change)
if num > 0:
color = 'green'
elif num < 0:
color = 'red'
percent = 100 * float(change) / (float(trade) - float(change))
data.append('<tr><td><a
href="http://finance.yahoo.com/q?s=%s">%s</a></td><td><font color="%s">%s (%s /
%.2f%%)</font></td></tr>' % (symbol, symbol, color, trade, change, percent))
return '<table>%s</table>' % ''.join(data)
except:
return '%s: Stock information unavailable' % symbols



bryan

--
http://mail.python.org/mailman/listinfo/python-list


fredrik at pythonware

Sep 14, 2006, 12:19 AM

Post #6 of 8 (1842 views)
Permalink
Re: stock quotes [In reply to]

Larry Bates wrote:

> If the HTML on the page(s) you want to process is clean and well formed it
> can be pretty easy. Download elementree from:
>
> http://effbot.org/zone/element-index.htm

and if it turns out to be messy, but you still want to use the same API,
see:

http://effbot.org/zone/element-soup.htm

</F>

--
http://mail.python.org/mailman/listinfo/python-list


anthra.norell at vtxmail

Sep 14, 2006, 4:44 AM

Post #7 of 8 (1844 views)
Permalink
Re: stock quotes [In reply to]

Donlingerfelt wrote:
> I would like to download stock quotes from the web, store them, do
> calculations and sort the results. However I am fairly new and don't have a
> clue how to parse the results of a web page download. I can get to the
> site, but do not know how to request the certain data need. Does anyone
> know how to do this? I would really appreciate it. Thanks.
>
>
>

Hi,

Heres's example 8.4 from the SE manual:

----------------------------------------------------------------------------------------------------------------------------------

> >> def get_current_stock_quotes (symbols):

import urllib

url = 'http://finance.yahoo.com/q/cq?d=v1&s=' + '+'.join (symbols)

htm_page = urllib.urlopen (url)

import SE

keep = '"~[A-Z]+ [JFMAJSOND].+?%~==(10)" "~[A-Z]+
[0-9][0-2]?:[0-5][0-9][AP]M.+?%~==(10)"'

Data_Extractor = SE.SE ('<EAT> ' + keep)

Tag_Stripper = SE.SE ('"~<(.|\n)*?>~= " se/htm2iso.se | "~\n[
\t\n]*~=(10)" "~ +~= "')

data = Data_Extractor (Tag_Stripper (htm_page.read ()))

htm_page.close ()

return data

> >> print get_current_stock_quotes (('GE','IBM','AAPL', 'MSFT', 'AA',
'MER'))

GE 3:17PM ET 33.15 0.30 0.90%

IBM 3:17PM ET 76.20 0.47 0.61%

AAPL 3:22PM ET 55.66 0.66 1.20%

MSFT 3:22PM ET 23.13 0.37 1.57%

AA 3:17PM ET 31.80 1.61 4.82%

MER 3:17PM ET 70.24 0.82 1.15%

-----------------------------------------------------------------------------------------------------------------------------

If this meets your requirements you'll find SE here:
http://cheeseshop.python.org/pypi/SE/2.2%20beta

Regards

Frederic

--
http://mail.python.org/mailman/listinfo/python-list


bulliver at gmail

Sep 14, 2006, 2:49 PM

Post #8 of 8 (1834 views)
Permalink
Re: stock quotes [In reply to]

On 9/13/06, Donlingerfelt <donlingerfelt [at] cox> wrote:
> I would like to download stock quotes from the web, store them, do
> calculations and sort the results. However I am fairly new and don't have a
> clue how to parse the results of a web page download. I can get to the
> site, but do not know how to request the certain data need. Does anyone
> know how to do this? I would really appreciate it. Thanks.

Some have already directed to webscraping tools, but you don't need to
go that route if you can settle for using finance.yahoo.com, as you
can ask for the data in a particular format ie: plain text, csv etc...

I have also written some code that does this. As an added bonus it
also does currency conversion. Maybe the code will help get you
started:
http://badcomputer.org/unix/code/stock.html

HTH
-d
--
http://badcomputer.org
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.