Python Quandl数据,API调用
最近我正在读一些Quandl中的股票价格数据库,使用API调用来提取数据。但我真的被我的例子弄糊涂了Python Quandl数据,API调用,python,api,request,quandl,Python,Api,Request,Quandl,最近我正在读一些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 =
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,如果我复制那个WebPage,它会说404找不到。所以,如果我想使用其他数据库,我该如何准备这个api_usl?股票是什么意思
2) 这里的请求看起来像是用来提取数据的,原始数据的格式是什么?我怎么知道列名?如何提取列?要展开我的上述评论:
%stock
是一种字符串格式化操作,将前面字符串中的%s
替换为stock
引用的值。详情请参阅raw_data
实际上引用了一个响应对象(属于请求
模块-找到详细信息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\u stock[column\u names]
将Date
和Open
分别显示为第一个和第二个值。这意味着它们对应于数据每个元素中的位置0
和1
因此,要访问日期,请使用aapl_库存['data'][0:10][0]
(前十项的日期值),并访问开放使用的值aapl_库存['data'][0:78][1]
(前78项的开放值)
要获取数据集中每个值的列表,其中每个元素都是一个包含日期和打开值的列表,您可以添加类似于aapl\u Date\u Open=aapl\u stock['data'][:][0:1]
的内容
如果您是python新手,我郑重建议您查看列表切片表示法,可以找到一个快速介绍无法对quandl数据格式进行评论,但是
%stock
是一个字符串格式操作(请参阅详细信息)。基本上,它是将前面字符串中的%s
替换为stock
引用的字符串。非常感谢您的帮助。我还有一个问题,如果我只想读取“Date”和“Open”列的数据,我应该使用什么命令?我尝试了aapl_stock['Date'][0:10],但它不起作用。因此aapl_stock[列名
]将Date
和Open
分别显示为第一个值和第二个值。这意味着它们对应于数据中每个元素的位置0
和1
。非常感谢。非常感谢。最后一个问题,我如何年复一年地读取数据?比如从1982-01-01到1982-12-31,这是1982年,s1983年的ame等。看起来quandl api支持日期范围(至少版本3支持)。如果您通过添加?start\u date=1982-01-01&end\u date=1982-12-31
来更改url,则您收到的数据将限制在该范围内。完整参考