python - Download stocks data from google finance -



python - Download stocks data from google finance -

i'm trying download info google finance list of stocks symbols within .csv file.

this class i'm trying adapt site:

import urllib,time,datetime import csv class quote(object): date_fmt = '%y-%m-%d' time_fmt = '%h:%m:%s' def __init__(self): self.symbol = '' self.date,self.time,self.open_,self.high,self.low,self.close,self.volume = ([] _ in range(7)) def append(self,dt,open_,high,low,close,volume): self.date.append(dt.date()) self.time.append(dt.time()) self.open_.append(float(open_)) self.high.append(float(high)) self.low.append(float(low)) self.close.append(float(close)) self.volume.append(int(volume)) def append_csv(self, filename): open(filename, 'a') f: f.write(self.to_csv()) def __repr__(self): homecoming self.to_csv() def get_symbols(self, filename): line in open(filename,'r'): if line != 'codigo': print line q = googlequote(line,'2014-01-01','2014-06-20') q.append_csv('data.csv') class googlequote(quote): ''' daily quotes google. date format='yyyy-mm-dd' ''' def __init__(self,symbol,start_date,end_date=datetime.date.today().isoformat()): super(googlequote,self).__init__() self.symbol = symbol.upper() start = datetime.date(int(start_date[0:4]),int(start_date[5:7]),int(start_date[8:10])) end = datetime.date(int(end_date[0:4]),int(end_date[5:7]),int(end_date[8:10])) url_string = "http://www.google.com/finance/historical?q={0}".format(self.symbol) url_string += "&startdate={0}&enddate={1}&output=csv".format( start.strftime('%b %d, %y'),end.strftime('%b %d, %y')) csv = urllib.urlopen(url_string).readlines() csv.reverse() bar in xrange(0,len(csv)-1): try: #ds,open_,high,low,close,volume = csv[bar].rstrip().split(',') #open_,high,low,close = [float(x) x in [open_,high,low,close]] #dt = datetime.datetime.strptime(ds,'%d-%b-%y') #self.append(dt,open_,high,low,close,volume) info = csv[bar].rstrip().split(',') dt = datetime.datetime.strftime(data[0],'%d-%b-%y') close = data[4] self.append(dt,close) except: print "error " + str(len(csv)-1) print "error " + csv[bar] if __name__ == '__main__': q = quote() # create generic quote object q.get_symbols('list.csv')

but, quotes, code doesn't homecoming info (e.g. biom3), fields homecoming '-'. how can handle split in these cases? last, @ point of script, stops of download info because script stops, doesn't homecoming message. how can handle problem?

it should work, notice ticker should be: bvmf:abre11

in [250]: import pandas.io.data web import datetime start = datetime.datetime(2010, 1, 1) end = datetime.datetime(2013, 1, 27) df=web.datareader("bvmf:abre11", 'google', start, end) print df.head(10) open high low close volume ?date 2011-07-26 19.79 19.79 18.30 18.50 1843700 2011-07-27 18.45 18.60 17.65 17.89 1475100 2011-07-28 18.00 18.50 18.00 18.30 441700 2011-07-29 18.30 18.84 18.20 18.70 392800 2011-08-01 18.29 19.50 18.29 18.86 217800 2011-08-02 18.86 18.86 18.60 18.80 154600 2011-08-03 18.90 18.90 18.00 18.00 168700 2011-08-04 17.50 17.85 16.50 16.90 238700 2011-08-05 17.00 17.00 15.63 16.00 253000 2011-08-08 15.50 15.96 14.35 14.50 224300 [10 rows x 5 columns] in [251]: df=web.datareader("bvmf:biom3", 'google', start, end) print df.head(10) open high low close volume ?date 2010-01-04 2.90 2.90 2.90 2.90 0 2010-01-05 3.00 3.00 3.00 3.00 0 2010-01-06 3.01 3.01 3.01 3.01 0 2010-01-07 3.01 3.09 3.01 3.09 2000 2010-01-08 3.01 3.01 3.01 3.01 0 2010-01-11 3.00 3.00 3.00 3.00 0 2010-01-12 3.00 3.00 3.00 3.00 0 2010-01-13 3.00 3.10 3.00 3.00 7000 2010-01-14 3.00 3.00 3.00 3.00 0 2010-01-15 3.00 3.00 3.00 3.00 1000 [10 rows x 5 columns]

python pandas web-scraping

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -