def getQuotesYahoo():
tickerStr = "GOOGL+AMZN"
yahoo_url ="http://finance.yahoo.com/d/quotes.csv?s=%s&f=saohgb3t1" % (tickerStr)
retQuotes = {}
data = urllib2.urlopen(yahoo_url).readlines()
for d in data:
p = d.strip().split(',')
stkInfo = {}
stkInfo['lastTime'] = p[6]
stkInfo['last'] = p[1]
stkInfo['open'] = p[2]
stkInfo['high'] = p[3]
stkInfo['low'] = p[4]
stkInfo['bid'] = p[5]
tic = p[0]
print stkInfo
retQuotes[tic] = stkInfo
print retQuotes['GOOGL']['last']
此代碼在KeyError上失敗,未使用字符串鍵填充字典。我基本上有相同的代碼爲googlefiance工作。Python失敗,發生密鑰錯誤
KeyError: 'GOOGL'
retQuotes:
{'"AMZN"': {'last': '594.60', 'bid': 'N/A', 'high': '597.86', 'low': '589.00', 'lastTime': '"4:00pm"', 'open': '594.32'}, '"GOOGL"': {'last': '759.98', 'bid': 'N/A', 'high': '767.13', 'low': '755.77', 'lastTime': '"4:00pm"', 'open': '765.87'}}
谷歌的股票代碼不是'GOOGL',它是'GOOG' – n1c9
'retQuotes'中的結果是什麼?打印變量可能會有所幫助。 –
看起來像股票是在結構中。 – user3763220