7

In python 2.7 shell I ran the follwoings:

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

Got error message on the 4th command as follows:

Traceback (most recent call last):
  Python Shell, prompt 3, line 1
  File "C:\Users\mlashkar\_development\python\v2.7\Lib\site-packages\googlefinance\__init__.py", line 70, in getQuotes
    content = json.loads(request(symbols))
  File "C:\Users\mlashkar\_development\python\v2.7\Lib\site-packages\googlefinance\__init__.py", line 33, in request
    resp = urlopen(req)
  File "C:\Users\mlashkar\_development\python\v2.7\Lib\urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\mlashkar\_development\python\v2.7\Lib\urllib2.py", line 435, in open
    response = meth(req, response)
  File "C:\Users\mlashkar\_development\python\v2.7\Lib\urllib2.py", line 548, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Users\mlashkar\_development\python\v2.7\Lib\urllib2.py", line 473, in error
    return self._call_chain(*args)
  File "C:\Users\mlashkar\_development\python\v2.7\Lib\urllib2.py", line 407, in _call_chain
    result = func(*args)
  File "C:\Users\mlashkar\_development\python\v2.7\Lib\urllib2.py", line 556, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 404: Not Found

Not sure whats going on. Here is an image of my activities. enter image description here

wkl
  • 77,184
  • 16
  • 165
  • 176
Polta Lashka
  • 105
  • 1
  • 12
  • Might be related to something recent with Google Finance's URL changes: https://github.com/hongtaocai/googlefinance/issues/39 – wkl Sep 06 '17 at 16:58
  • Yeah @birryree, it seems freshly reported issue ... – Polta Lashka Sep 06 '17 at 17:11
  • I've posted an example of how to get the data directly rather than use a module, if that helps you. – wkl Sep 06 '17 at 17:55

3 Answers3

13

It seems like Google Finance modified their URLs/endpoints and the googlefinance package has not been updated to reflect the change.

Since most of these changes are rather opaque to end-users (and the library you're using hasn't been updated in 2 years), you might have better luck dealing with the raw Google Finance response yourself.

The Google Finance Endpoint

You can retrieve information about a particular ticker symbol via the following URL:

https://finance.google.com/finance?output=json&q=TICKER_SYMBOL

The Response

Google Finance returns JSON results in this format

\n// [\n{\n"symbol" : "AAPL",\n"exchange" : "NASDAQ",\n"id": "22144",\n"t" 
: "AAPL",\n"e" : "NASDAQ",\n"name" : "Apple Inc."\n, "f_reuters_url" : 
"http:\\x2F\\x2Fstocks.us.reuters.com\\x2Fstocks\\x2Fratios.asp?rpc=66\\x26symbol=AAPL.O",\n"f_recent_quarter_date" : "Q3 (Jul \\x2717)",\n"f_annual_date" : "2016",\n"f_ttm_date" : "2015",\n"financials" :

    ... a lot more stuff ...
[\n]\n}]\n'

It can't be loaded by Python's JSON parser as-is because it has leading //, and wraps everything inside []. It also has Unicode-escaped characters in various strings that need to be decoded.

Complete code and parsing

I'm going to use the requests module for this, but if you want an example with the built-in urllib module, I can show that as well.

import json

import requests

rsp = requests.get('https://finance.google.com/finance?q=AAPL&output=json')

if rsp.status_code in (200,):

    # This magic here is to cut out various leading characters from the JSON 
    # response, as well as trailing stuff (a terminating ']\n' sequence), and then
    # we decode the escape sequences in the response
    # This then allows you to load the resulting string
    # with the JSON module.
    fin_data = json.loads(rsp.content[6:-2].decode('unicode_escape'))

    # print out some quote data
    print('Opening Price: {}'.format(fin_data['op']))
    print('Price/Earnings Ratio: {}'.format(fin_data['pe']))
    print('52-week high: {}'.format(fin_data['hi52']))
    print('52-week low: {}'.format(fin_data['lo52']))

This would output:

Opening Price: 162.71
Price/Earnings Ratio: 18.43
52-week high: 164.94
52-week low: 102.53

There is a lot more data that's included in a full ticker JSON than what I'm outputting, so it's up to you to decide how you want to use any of it.

Alternatives

Alternatively, you could use the yahoo-finance module, which is probably less likely to have issues like this as Yahoo still provide a real finance API.

wkl
  • 77,184
  • 16
  • 165
  • 176
  • Simply awesome @birryree! Thanks for great details ! – Polta Lashka Sep 06 '17 at 19:36
  • @birryree This endpoint does not support multiple query at one time? – bkcollection Sep 07 '17 at 04:01
  • @bkcollection unfortunately, no. I don't know if there's a replacement for the endpoint that the module used to use. – wkl Sep 07 '17 at 04:02
  • @birryree Thanks. Any other way that we can get a list of quotes at one query now with google? – bkcollection Sep 07 '17 at 06:41
  • One issues with `http://finance.yahoo.com/d/quotes.csv?s=AAPL+GOOG+MSFT&f=snbaopl1` is the time of the stock quote, will sometimes going back to the previous one after refresh, mean I get the quote at 11am, then 11.05am I refresh again but sometimes the stock quote get data for 10.50am or even back to yesterday's quote. Not sure if someone have other workaround. – bkcollection Sep 07 '17 at 06:48
  • There are someone point out the same issue for Yahoo finance quote over here https://stackoverflow.com/questions/39177284/yahoo-finance-api-quotes-wrong-date-randomly-between-today-and-yesterday-days – bkcollection Sep 07 '17 at 07:49
  • Most info works, although the ticker xxxx:1234 might have changed. Also, i don't get data yet for currency like EURUSD:CURRENCY. Still searching. – aldwinaldwin Sep 07 '17 at 09:59
  • Similar issue reported, some alternate suggested there could be helpful. https://stackoverflow.com/questions/46071595/google-finance-api-for-getting-quote-has-stopped-working-today-is-there-an-alte/46073124#46073124 – Amit Sep 08 '17 at 00:54
  • 1
    fin_data = json.loads(rsp.content[6:-2].decode('unicode_escape')) This has stopped working as of 20-March-2018 for NSE (India) Stocks (url - https://finance.google.com/finance?q=NSE:INFY). Seems google changed what is returned as response when you are scrapping. In browser , it still asks to save a file which has json.. – Amit Mar 20 '18 at 15:01
1

If you are using Python 3.6 or 2.7, try using: Quandl https://www.quandl.com/ Use WIKI it seems to be stable example: Apple = quandl.get('WIKI/AAPL', start_date="2016-12-31", end_date="") Time series docs: https://docs.quandl.com/docs/time-series-2 If you make more than 50 requests Quandl requires a key(free to use)

ShaneCalder
  • 316
  • 2
  • 5
0

Multiple stock details working on this endpoint with google stock ID

https://finance.google.com/finance/data?dp=mra&output=json&catid=all&cid=13564339,5904015
Anish Manchappillil
  • 697
  • 2
  • 10
  • 19