0

I'm extracting data from Y!F using yahoo_finance module. A type error:

'Share' object does not support indexing

is occurring.

Any ideas?

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
2
  • Please provide the full traceback, not just the last line. Commented Jul 20, 2015 at 7:43
  • I'd bet the problem is shadowing the list symbol = ['GOOG', ...] with the Share instance symbol = Share(...)... Commented Jul 20, 2015 at 7:45

2 Answers 2

1

You are reassigning your list symbol to:

symbol = Share(stringSymb)

then next loop, you are trying to index symbol

stringSymb = symbol[cc]

And as the error states, the Share object that you reassigned symbol to, does not support indexing.

Sign up to request clarification or add additional context in comments.

Comments

0

You're using symbol twice:

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

And in the loop:

symbol = Share(stringSymb)

It seems that Share from yahoo_finance does not support indexing.

Try changing it to the following:

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

Output:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.