python 2.7 中来自 googlefinance 的 HTTP 错误 404
HTTP Error 404 from googlefinance in python 2.7
在 python 2.7 shell 我 运行 以下内容:
$from googlefinance import getQuotes
$import json
$from urllib2 import urlopen
$print json.dumps(getQuotes('AAPL'), indent=2)
在第 4 个命令中收到如下错误消息:
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
不确定发生了什么。
这是我的活动图片。
似乎 Google Finance 修改了他们的 URLs/endpoints 并且 googlefinance
包尚未更新以反映更改。
由于这些更改中的大多数对最终用户来说相当不透明(并且您使用的库已经 2 年没有更新),因此您可能更幸运地处理原始 Google 财务自己回复。
Google 财务端点
您可以通过以下方式检索有关特定股票代码的信息 URL:
https://finance.google.com/finance?output=json&q=TICKER_SYMBOL
回应
Google 财务 returns JSON 结果采用这种格式
\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'
Python 的 JSON 解析器无法按原样加载它,因为它有前导 //
,并将所有内容包装在 []
中。它还在需要解码的各种字符串中包含 Unicode 转义字符。
完整的代码和解析
我将为此使用 requests
模块,但如果您想要一个内置 urllib
模块的示例,我也可以展示。
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']))
这将输出:
Opening Price: 162.71
Price/Earnings Ratio: 18.43
52-week high: 164.94
52-week low: 102.53
完整代码 JSON 中包含的数据比我输出的要多得多,因此您可以决定如何使用其中的任何数据。
备选方案
或者,您可以使用 yahoo-finance
模块,由于雅虎仍然提供真正的金融服务,因此出现此类问题的可能性较小 API。
如果您使用的是 Python 3.6 或 2.7,请尝试使用:
宽德尔 https://www.quandl.com/
使用 WIKI 似乎很稳定
例子:
苹果 = quandl.get('WIKI/AAPL', start_date="2016-12-31", end_date="")
时间序列文档:
https://docs.quandl.com/docs/time-series-2
如果你发出超过50个请求,Quandl需要一个密钥(免费使用)
使用 google 股票 ID
在该端点上工作的多个股票详细信息
https://finance.google.com/finance/data?dp=mra&output=json&catid=all&cid=13564339,5904015
在 python 2.7 shell 我 运行 以下内容:
$from googlefinance import getQuotes
$import json
$from urllib2 import urlopen
$print json.dumps(getQuotes('AAPL'), indent=2)
在第 4 个命令中收到如下错误消息:
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
不确定发生了什么。
这是我的活动图片。
似乎 Google Finance 修改了他们的 URLs/endpoints 并且 googlefinance
包尚未更新以反映更改。
由于这些更改中的大多数对最终用户来说相当不透明(并且您使用的库已经 2 年没有更新),因此您可能更幸运地处理原始 Google 财务自己回复。
Google 财务端点
您可以通过以下方式检索有关特定股票代码的信息 URL:
https://finance.google.com/finance?output=json&q=TICKER_SYMBOL
回应
Google 财务 returns JSON 结果采用这种格式
\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'
Python 的 JSON 解析器无法按原样加载它,因为它有前导 //
,并将所有内容包装在 []
中。它还在需要解码的各种字符串中包含 Unicode 转义字符。
完整的代码和解析
我将为此使用 requests
模块,但如果您想要一个内置 urllib
模块的示例,我也可以展示。
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']))
这将输出:
Opening Price: 162.71
Price/Earnings Ratio: 18.43
52-week high: 164.94
52-week low: 102.53
完整代码 JSON 中包含的数据比我输出的要多得多,因此您可以决定如何使用其中的任何数据。
备选方案
或者,您可以使用 yahoo-finance
模块,由于雅虎仍然提供真正的金融服务,因此出现此类问题的可能性较小 API。
如果您使用的是 Python 3.6 或 2.7,请尝试使用: 宽德尔 https://www.quandl.com/ 使用 WIKI 似乎很稳定 例子: 苹果 = quandl.get('WIKI/AAPL', start_date="2016-12-31", end_date="") 时间序列文档: https://docs.quandl.com/docs/time-series-2 如果你发出超过50个请求,Quandl需要一个密钥(免费使用)
使用 google 股票 ID
在该端点上工作的多个股票详细信息https://finance.google.com/finance/data?dp=mra&output=json&catid=all&cid=13564339,5904015