Skip to content

get_stock_data.py

#!/usr/bin/python

import getopt
from getpass import getpass
import atom, atom.service
try:
from xml.etree import ElementTree
except ImportError:
from elementtree import ElementTree
import gdata.spreadsheet.service
from BeautifulSoup import BeautifulSoup
import urllib2, re
import csv

#Usage
#ibm = stock_page(”IBM”)
#stock_values[ibm['symbol']] = ibm['history']

class stock_page:
def __init__(self, symbol, exchange=”"):
self.exchange = exchange
self.symbol = symbol
self.historical_data = {}
self.lookup_url = “”
self.history_url = “”
self.prices_url = “”

def __getitem__(self, key):
try:
if key == ’symbol’:
return self.symbol
if key == ‘exchange’:
return self.exchange
if not self.historical_data:
self._get_history()
if key == ‘history’:
return self.historical_data
return self.historical_data[key]
except KeyError:
return None

def _get_info_url(self):
if not self.history_url:
self.lookup_url = “http://finance.google.com/finance?q=%s” % (self.symbol)
main_page = BeautifulSoup(urllib2.urlopen(self.lookup_url))
id = main_page.find(”tr”, “hdgr”).td.contents[2].split()[2].split(”:”)
self.exchange = id[0]
self.symbol = id[1].split(”)”)[0]
self.history_url = “http://finance.google.com/finance/historical?q=%s:%s” % (self.exchange, self.symbol)

def _get_history_url(self):
if not self.history_url:
self._get_info_url()
if self.history_url and not self.prices_url:
history_page = BeautifulSoup(urllib2.urlopen(self.history_url))
l = history_page.find(”a”, text=”Download to spreadsheet”)
self.prices_url = l.parent['href']

def _get_history(self):
if not self.prices_url:
self._get_history_url()
if self.prices_url and not self.historical_data:
dr = csv.DictReader(urllib2.urlopen(self.prices_url))
self.historical_data = dict( [(x['Date'],x['Close']) for x in dr ])
if not self.historical_data:
print “No History Data… something’s probably wrong”

if __name__ == “__main__”:
stock_values = {}
stocks = (’GOOG’, ‘IBM’, ‘JDSU’, ‘HPQ’, ‘GE’, ‘MSFT’, ‘BUD’, ‘DIS’)
stocks = (”GOOG”,)
for sym in stocks:
s = stock_page(sym)
stock_values[s['symbol']] = s['history']

print stock_values