最近,我正在阅读Quandl中的一些股票价格数据库,使用API调用来提取数据。但我真的对我的例子感到困惑。
import requests
api_url = 'https://www.quandl.com/api/v1/datasets/WIKI/%s.json' % stock
session = requests.Session()
session.mount('http://', requests.adapters.HTTPAdapter(max_retries=3))
raw_data = session.get(api_url)有人能跟我解释一下吗?
( 1)对于api_url,如果我复制该漏洞,它会显示404未找到。所以,如果我想使用其他数据库,我如何准备这个api_usl?'%股票‘是什么意思?
2)在这里,请求看起来是用来提取数据的,raw_data的格式是什么?我怎么知道列名的?如何提取列?
发布于 2016-01-29 00:48:50
关于我上面的评论,我想说的是:
% stock是一个字符串格式化操作,用stock引用的值替换前一个字符串中的%s。更详细的信息可以找到这里raw_data实际上引用了一个响应对象( requests模块的一部分--找到这里的详细信息)。扩展您的代码。
import requests
#Set the stock we are interested in, AAPL is Apple stock code
stock = 'AAPL'
#Your code
api_url = 'https://www.quandl.com/api/v1/datasets/WIKI/%s.json' % stock
session = requests.Session()
session.mount('http://', requests.adapters.HTTPAdapter(max_retries=3))
raw_data = session.get(api_url)
# Probably want to check that requests.Response is 200 - OK here
# to make sure we got the content successfully.
# requests.Response has a function to return json file as python dict
aapl_stock = raw_data.json()
# We can then look at the keys to see what we have access to
aapl_stock.keys()
# column_names Seems to be describing the individual data points
aapl_stock['column_names']
# A big list of data, lets just look at the first ten points...
aapl_stock['data'][0:10]编辑在评论中回答问题
因此,aapl_stock[column_names]将Date和Open分别显示为第一和第二值。这意味着它们对应于数据的每个元素中的0和1位置。
因此,要访问日期,请使用aapl_stock['data'][0:10][0] (前十项的日期值),并访问开放使用aapl_stock['data'][0:78][1] (前78项的开放值)的值。
要获得dataset中每个值的列表,其中每个元素都是一个包含日期和打开值的列表,您可以添加类似于aapl_date_open = aapl_stock['data'][:][0:1]的内容。
如果您是python的新手,我建议您看一下列表切片表示法,可以找到一个快速的介绍这里。
https://stackoverflow.com/questions/35073773
复制相似问题