stock quotes

D

Donlingerfelt

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.
 
G

George Sakkis

Donlingerfelt said:
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
 
L

Larry Bates

Donlingerfelt said:
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
 
S

skip

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
 
B

Bryan

Donlingerfelt said:
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
 
F

Frederic Rentsch

Donlingerfelt said:
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:

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

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
'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 beta

Regards

Frederic
 
D

Darren Kirby

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top