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
// [
{
"symbol" : "AAPL",
"exchange" : "NASDAQ",
"id": "22144",
"t"
: "AAPL",
"e" : "NASDAQ",
"name" : "Apple Inc."
, "f_reuters_url" :
"http:\x2F\x2Fstocks.us.reuters.com\x2Fstocks\x2Fratios.asp?rpc=66\x26symbol=AAPL.O",
"f_recent_quarter_date" : "Q3 (Jul \x2717)",
"f_annual_date" : "2016",
"f_ttm_date" : "2015",
"financials" :
... a lot more stuff ...
[
]
}]
'
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 ']
' 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.