Starting with this from Corey Goldberg:
#!/usr/bin/env python
import json
import pprint
import urllib2
def get_stock_quote(ticker_symbol):
url = 'http://finance.google.com/finance/info?q=%s' % ticker_symbol
lines = urllib2.urlopen(url).read().splitlines()
return json.loads(''.join([x for x in lines if x not in ('// [', ']')]))
if __name__ == '__main__':
quote = get_stock_quote('IBM')
print 'ticker: %s' % quote['t']
print 'current price: %s' % quote['l_cur']
print 'last trade: %s' % quote['lt']
print 'full quote:'
pprint.pprint(quote)
Using this:
import urllib2, json
def get_stock_quote(ticker_symbol):
url = 'http://finance.google.com/finance/info?q=%s' % ticker_symbol
lines = urllib2.urlopen(url).read().splitlines()
#print lines
return json.loads(''.join([x for x in lines if x not in ('// [', ']')]))
if __name__ == '__main__':
symbols = ('Goog',)
symbols2 = ('Goog','MSFT')
quote = get_stock_quote(symbols)
print 'ticker: %s' % quote['t'], 'current price: %s' % quote['l_cur'], 'last trade: %s' % quote['ltt']
print quote['t'], quote['l'], quote['ltt']
Usings symbols works, symbols2 does not work. The error message is
TypeError: not all arguments converted during string formatting
How do I convert all arguments to string in string formatting. In browser, the code that works is: Goog,MSFT.
EDIT: the output I am looking for is a list with goog, msft info.