user4723143
user4723143

Reputation: 1

Retrieving Stock Data using Google Finance 0.5 pypi

I downloaded the Google Finance and was wondering how I could extract specific values versus all of them. This is the example given:

>>> from googlefinance import getQuotes
>>> print json.dumps(getQuotes('AAPL'), indent=2)

[
  {
    "Index": "NASDAQ", 
    "LastTradeWithCurrency": "123.25", 
    "LastTradeDateTime": "2015-03-27T16:03:28Z", 
    "LastTradePrice": "123.25", 
    "LastTradeTime": "4:03PM EDT", 
    "LastTradeDateTimeLong": "Mar 27, 4:03PM EDT", 
    "StockSymbol": "AAPL", 
    "ID": "22144"
  }
]

What would the

>>> print json.dumps(get(…)('AAPL'), indent=2)

call be if I just wanted to call the stock dividend for example?

Upvotes: 0

Views: 2677

Answers (2)

zelusp
zelusp

Reputation: 3688

Assume stock = json.dumps(getQuotes('AAPL'), indent=2). Then, type(stock) returns type string.

What you need to do is skip encoding it as JSON since getQuotes('AAPL') already outputs what you want as a list. Instead, save the output like so:

stock=getQuotes('AAPL')

Then, as bakkal pointed out, you can address specific values of the list via indexing e.g. stock[0].

print  stock[0]
{u'Index': u'NASDAQ', u'LastTradeWithCurrency': u'113.40', u'LastTradeDateTime': u'2015-09-28T13:49:28Z', u'LastTradePrice': u'113.40', u'LastTradeTime': u'1:49PM EDT', u'LastTradeDateTimeLong': u'Sep 28, 1:49PM EDT', u'StockSymbol': u'AAPL', u'ID': u'22144'}

You'll want to drill down to 'dividends', or in my example, 'LastTradePrice' by using

stock[0]['LastTradePrice']

This returns:

u'113.40'

Now, also as bakkal pointed out, getting our value as a number is simply a question of using float(stock[0]['LastTradePrice'])

Update The above was confirmed to work on a RaspberryPi Running Debian Wheezy. As demonstrated by bakkal (and confirmed on my Windows machine) stock['LastTradePrice'] may be the correct form of indexing your list depending on your platform.

Upvotes: 1

bakkal
bakkal

Reputation: 55448

was wondering how I could extract specific values versus all of them

If your call returns

x = [
  {
    "Index": "NASDAQ",
    "LastTradeWithCurrency": "129.09",
    "LastTradeDateTime": "2015-03-02T16:04:29Z",
    "LastTradePrice": "129.09",
    "Yield": "1.46",
    "LastTradeTime": "4:04PM EST",
    "LastTradeDateTimeLong": "Mar 2, 4:04PM EST",
    "Dividend": "0.47",
    "StockSymbol": "AAPL",
    "ID": "22144"
  }
]

Then x is a list, the first element in the list is e.g.stock = x[0], which is:

{
        "Index": "NASDAQ",
        "LastTradeWithCurrency": "129.09",
        "LastTradeDateTime": "2015-03-02T16:04:29Z",
        "LastTradePrice": "129.09",
        "Yield": "1.46",
        "LastTradeTime": "4:04PM EST",
        "LastTradeDateTimeLong": "Mar 2, 4:04PM EST",
        "Dividend": "0.47",
        "StockSymbol": "AAPL",
        "ID": "22144"
      }

So to get the dividend you can access the key 'Dividend', stock['Dividend'], that will get your the dividend value as a string, you can convert it to a float e.g. float(stock['Dividend'])

Also Try this yahoo-finance lib https://github.com/lukaszbanasiak/yahoo-finance, as it has a get_dividend_share() (dividends per share), and get_dividend_yield() (annual dividends / stock price, in %)

Upvotes: 1

Related Questions