Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old September 14th, 2006, 02:45 AM
Donlingerfelt
Guest
 
Posts: n/a
Default 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.


  #2  
Old September 14th, 2006, 03:15 AM
George Sakkis
Guest
 
Posts: n/a
Default Re: stock quotes

Donlingerfelt wrote:
Quote:
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

  #3  
Old September 14th, 2006, 03:25 AM
Larry Bates
Guest
 
Posts: n/a
Default Re: stock quotes

Donlingerfelt wrote:
Quote:
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
  #4  
Old September 14th, 2006, 04:25 AM
skip@pobox.com
Guest
 
Posts: n/a
Default Re: stock quotes


donI would like to download stock quotes from the web, store them, do
doncalculations and sort the results. However I am fairly new and
dondon't have a clue how to parse the results of a web page download.
donI can get to the site, but do not know how to request the certain
dondata need. Does anyone know how to do this? I would really
donappreciate 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
  #5  
Old September 14th, 2006, 05:35 AM
Bryan
Guest
 
Posts: n/a
Default Re: stock quotes

Donlingerfelt wrote:
Quote:
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

  #6  
Old September 14th, 2006, 08:15 AM
Fredrik Lundh
Guest
 
Posts: n/a
Default Re: stock quotes

Larry Bates wrote:
Quote:
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>

  #7  
Old September 14th, 2006, 12:35 PM
Frederic Rentsch
Guest
 
Posts: n/a
Default Re: stock quotes

Donlingerfelt wrote:
Quote:
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:

----------------------------------------------------------------------------------------------------------------------------------
Quote:
Quote:
Quote:
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
Quote:
Quote:
Quote:
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

  #8  
Old September 14th, 2006, 10:35 PM
Darren Kirby
Guest
 
Posts: n/a
Default Re: stock quotes

On 9/13/06, Donlingerfelt <donlingerfelt@cox.netwrote:
Quote:
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
 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles