how to get a stock historical value from google finance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | import urllib2
import re
from table_parser import *
"""
By bussiere bussiere @at gmail.com
thanks to :
http://simbot.wordpress.com/2006/05/17/html-tables-parsed-using-python/
Nigel Sim <nigel.sim @at gmail.com>
http://simbot.wordpress.com
"""
__Author__ ="bussiere"
__Email__ = "bussiere @at gmail.com"
__Titre__ = "get some value with google finance"
__Description__ = "get the historical value on google finance"
__Discussion__ = "A beginnig for a stock analyze program."
__Tags__ ="google fiance stock value historical"
def get_finance(value):
#we make a url for google finance with the value given
link = "http://finance.google.com/finance?q=%s"%value
#we open this url
page = urllib2.urlopen(link).read()
#we find where is the hisctorical link
findhistorical = re.findall("/finance/historical\?q.*\"", page)
print findhistorical
#we substract the " at the end of the string
findhistorical = findhistorical[0].replace('\"','')
#we make a link for the historical page of the value
histlink = "http://finance.google.com%s"%findhistorical
#we open the historical page
hist = urllib2.urlopen(histlink).read()
#we find the link for getting the data in csv mode
findcsv = re.findall('http://finance.*csv',hist)
#we open the link
csv = ''
try :
#we try to get the csv file if existent
csv = urllib2.urlopen(findcsv[0]).read()
except :
#else we parse the google finance page with table parser
findcsv = re.findall('<div id=prices>.*?</table>',hist,re.S)
p = TableParser()
p.feed(findcsv[0])
csv = p.doc
#we return the csv data for the value
return csv
def main(argv=None):
# we get the argument passed on the command line
value = sys.argv[1]
print get_finance(value)
if __name__ == "__main__":
import sys
#we call the main function
sys.exit(main())
|
the beginnig of a stock value analysys program
Nice but where table_parser came from?
I used the csv output from http://ichart.finance.yahoo.com/table.csv: here a multithreaded example:
Look at itrade (written in Python) that have lots of financial interface. It also supports proxy and handle timeout connexion.
http://itrade.sf.net
Hi,Whats the use of This function(def get_finance(value):).In below you are not call the function any where.From where you are get this table(table_parser).
http://www.samestock.com
Hi,
I just copied and pasted Michele Bertoldi's logic but it gives me syntaxError on print '%s: %i quotes'%(k,len(quotes[k]['closes'])). Could anyone tell me why I am getting syntaxError ?
Thanks, jd
John, this is the correct version: