akshare地址:https://www.akshare.xyz/data/index.html
常用的包导入:
import akshare as ak import numpy as np import pandas as pd from datetime import datetime import matplotlib.pyplot as plt import mplfinance as mpf from pylab import mpl mpl.rcParams['font.sans-serif'] = ['SimHei'] # 设置中文字体为微软雅 plt.rcParams['font.sans-serif'] = ['SimHei'] # 字体设置 import matplotlib matplotlib.rcParams['axes.unicode_minus']=False # 负号显示问题
一、获取沪深300指数历史数据:
security ="000300" begin_date ='20000101' end_date = datetime.now().strftime('%Y%m%d') df = ak.index_zh_a_hist(symbol =security,period='daily',start_date=begin_date,end_date= end_date) df
运行结果:
二、获取股票历史数据:
end =datetime.now().strftime('%Y%m%d') #code= str(stock_pool['代码'].values[0]) code = '600309' data = ak.stock_zh_a_hist(symbol=str(code), period="daily", start_date="19900301", end_date=end, adjust="") data['日期'] = pd.to_datetime(data['日期'], format='%Y-%m-%d') data.rename(columns= {'日期':'date','开盘':'open','最高':'high','最低':'low','收盘':'close','成交量':'volume','涨跌幅':'pct_chg'},inplace=True) data['code'] = code #data = data[['date','open','close','high','low','volume','code']] data = data.set_index(data.date)
运行结果:
三、获取期货数据:
#获取股指期货数据 df3 = ak.futures_main_sina(symbol="IF0") df3.rename(columns={"日期":'trade_date',"开盘价":'open',"收盘价":'close',"最高价":'high',"最低价":'low'},inplace = True) df3.sort_index()
标签:code,end,抓取,接口,datetime,akshare,date,import,data From: https://www.cnblogs.com/leon507/p/17443171.html