HTTP Error 404 from googlefinance in python 2.7
在 python 2.7 shell 中,我运行了以下命令:
1
2 3 4 |
$from googlefinance import getQuotes
$import json $from urllib2 import urlopen $print json.dumps(getQuotes(‘AAPL’), indent=2) |
第 4 条命令出现错误信息如下:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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 的 URL 更改最近发生的事情有关:github.com/hongtaocai/googlefinance/issues/39
- 是的@birryree,这似乎是新报道的问题……
- 如果对您有帮助,我已经发布了一个如何直接获取数据而不是使用模块的示例。
Google 财经似乎修改了他们的网址/端点,而 googlefinance 包尚未更新以反映更改。
由于这些更改中的大多数对最终用户来说相当不透明(而且您使用的库在 2 年内没有更新),因此您自己处理原始的 Google 财经响应可能会更好。
Google 财经端点
您可以通过以下 URL 检索有关特定股票代码的信息:
1
|
https://finance.google.com/finance?output=json&q=TICKER_SYMBOL
|
响应
Google 财经以这种格式返回 JSON 结果
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
\
// [\ {\ “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 … |
它不能被 Python 的 JSON 解析器按原样加载,因为它具有前导 //,并将所有内容package在 [] 中。它还在需要解码的各种字符串中包含 Unicode 转义字符。
完整的代码和解析
我将为此使用 requests 模块,但是如果您想要一个带有内置 urllib 模块的示例,我也可以展示它。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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 # print out some quote data |
这将输出:
1
2 3 4 |
Opening Price: 162.71
Price/Earnings Ratio: 18.43 52-week high: 164.94 52-week low: 102.53 |
完整代码 JSON 中包含的数据比我输出的要多得多,因此您可以决定如何使用其中的任何一个。
备择方案
或者,您可以使用 yahoo-finance 模块,因为 Yahoo 仍然提供真正的金融 API,所以它可能不太可能出现此类问题。
- 简直太棒了@birryree!感谢您提供详细信息!
- @birryree 这个端点不支持一次多个查询?
- @bkcollection 不幸的是,没有。我不知道模块过去使用的端点是否有替代品。
- @birryree 谢谢。我们现在可以通过谷歌在一次查询中获得报价列表的任何其他方式吗?
- http://finance.yahoo.com/d/quotes.csv?s=AAPL+GOOG+MSFT&f=snb??aopl1 的一个问题是股票报价的时间,有时会在刷新后回到前一个,这意味着我在上午 11 点获得报价,然后在上午 11.05 再次刷新,但有时股票报价会在上午 10.50 或甚至回到昨天的报价。不确定是否有人有其他解决方法。
- 有人在此处stackoverflow.com/questions/39177284/上指出了雅虎财务报价的相同问题…
- 大多数信息都有效,尽管代码 xxxx:1234 可能已更改。此外,我还没有获得 EURUSD:CURRENCY 等货币的数据。仍在搜索中。
- 报告了类似的问题,一些替代建议可能会有所帮助。 stackoverflow.com/questions/46071595/…
- fin_data = json.loads(rsp.content[6:-2].decode(\\’unicode_escape\\’)) 自 2018 年 3 月 20 日起,NSE(印度)股票已停止工作(url – finance.google.com /财务?q=NSE:INFY)。当您报废时,谷歌似乎更改了作为响应返回的内容。在浏览器中,它仍然要求保存一个包含 json 的文件。
如果您使用的是 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 需要一个密钥(免费使用)
在此端点上使用谷歌股票 ID
工作的多个股票详细信息
1
|
https://finance.google.com/finance/data?dp=mra&output=json&catid=all&cid=13564339,5904015
|
来源:https://www.codenong.com/46080632/