2015-07-20 159 views
0

我使用yahoo_finance模块从Y!F提取数据。 A型错误:Python,不支持索引的对象

'Share' object does not support indexing

正在发生。

任何想法?

import yahoo_finance 
from yahoo_finance import Share 

class ticker(object): 
    def __init__(self, symbol): 

     self.price = symbol.get_price() 
     self.change = symbol.get_change() 
     self.volume = symbol.get_volume() 

symbol = ['GOOG','AAPL','MSFT'] 

lenSymbol = len(symbol) 
cc = 0 

while cc < lenSymbol: 
    stringSymb = symbol[cc] 
    symbol = Share(stringSymb) #TypeError occurring here 
    c = ticker(symbol) 
    output = ([c.volume, c.price, c.change, c.volume]) 
    print (output) 
    cc += 1 
+0

请提供*全回溯*,而不仅仅是最后一行。 –

+0

我敢打赌,这个问题是通过'Share'实例'symbol = Share(...)'来勾画出符号= ['GOOG',...]'的列表...... – jonrsharpe

回答

0

您使用symbol两次:

symbol = ['GOOG','AAPL','MSFT'] 

而且在循环:

symbol = Share(stringSymb) 

似乎Shareyahoo_finance不支持索引。

尝试将其更改为以下:

import yahoo_finance 
from yahoo_finance import Share 

class ticker(object): 
    def __init__(self, symbol): 
     self.price = symbol.get_price() 
     self.change = symbol.get_change() 
     self.volume = symbol.get_volume() 

symbols = ['GOOG','AAPL','MSFT'] 

cc = 0 
while cc < len(symbols): 
    stringSymb = symbols[cc] 
    symbol = Share(stringSymb) 
    c = ticker(symbol) 
    output = ([c.volume, c.price, c.change, c.volume]) 
    print (output) 
    cc += 1 

输出:

['11164943', '672.93', '+93.08', '11164943'] 
['46164710', '129.62', '+1.11', '46164710'] 
['29467107', '46.62', '-0.04', '29467107'] 
1

您重新分配列表symbol到:

symbol = Share(stringSymb) 

然后下一个循环,你正试图指数symbol

stringSymb = symbol[cc] 

而作为错误状态时,Share对象,您重新分配symbol,不支持索引。