HTTP 404 error from googlefinance in python 2.7

In python 2.7 shell, I completed the following tasks:

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

The 4th command error message is received 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 what is going on. Here is an image of my activity. enter image description here

+5
json python google-finance
source share
3 answers

It looks like Google Finance has changed its URLs / endpoints, and the googlefinance package googlefinance not been updated to reflect this change.

Since most of these changes are pretty opaque to end users (and the library you use has not been updated after 2 years), you might be better off with a raw Google Finance answer.

Google Finance Endpoint

You can get information about a specific ticker symbol at the following URL:

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

Answer

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 cannot be loaded with the Python JSON as-is parser because it has a leading // and wraps everything inside [] . It also has Unicode-escaped characters in various lines that need to be decoded.

Full code and parsing

I am going to use the requests module for this, but if you need an example with a built-in urllib module, I can also show this.

 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 will lead to the conclusion:

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

There is much more data that is included in the full JSON code than what I output, so it's up to you how you want to use it.

Alternatives

Alternatively, you can use the yahoo-finance module, which is probably less likely to have problems like Yahoo still provides real API funding.

+11
source

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

+1
source

Some stock data working with this endpoint with google stock id

 https://finance.google.com/finance/data?dp=mra&output=json&catid=all&cid=13564339,5904015 
0
source

All Articles