python金融数据分析和可视化--03利用Akshare获取股票数据

02利用Akshare获取股票数据

1. AKShare 的介绍

AKShare 是基于 Python 的财经数据接口库,目的是实现对股票、期货、期权、基金、外汇、债券、指数、加密货币等金融产品的基本面数据、实时和历史行情数据、衍生数据从数据采集、数据清洗到数据落地的一套工具,主要用于学术研究目的。

AKShare 的特点是获取的是相对权威的财经数据网站公布的原始数据,通过利用原始数据进行各数据源之间的交叉验证,进而再加工,从而得出科学的结论。

2. 安装 AKShare

pip install akshare

3. 获取股票数据

AKShare 股票数据
AKShare github

# 股票市场总貌
import akshare as ak


# 上海证券交易所
# http://www.sse.com.cn/market/stockdata/statistic/
def sh_df():
    stock_sse_summary_df = ak.stock_sse_summary()
    print(stock_sse_summary_df)


# 深圳证券交易所
# 证券类别统计
# http://www.szse.cn/market/overview/index.html
def sz_df():
    stock_szse_summary_df = ak.stock_szse_summary()
    print(stock_szse_summary_df)


# 深圳证券交易所
# 地区交易排序
# http://www.szse.cn/market/overview/index.html
def sz_area():
    stock_szse_area_summary_df = ak.stock_szse_area_summary(date="202203")
    print(stock_szse_area_summary_df)


# 深圳证券交易所
# 股票行业成交
# http://docs.static.szse.cn/www/market/periodical/month/W020220511355248518608.html
def sz_sector():
    stock_szse_sector_summary_df = ak.stock_szse_sector_summary(symbol="当年", date="202204")
    print(stock_szse_sector_summary_df)

# 上海证券交易所
# 每日概况
#  http://www.sse.com.cn/market/stockdata/overview/day/
def sh_day():
    stock_sse_deal_daily_df = ak.stock_sse_deal_daily(date="20201111")
    print(stock_sse_deal_daily_df)


def get_account_statistics():
    # 股票账户统计月度
    """
    输出参数
    名称    类型    描述
    数据日期    object    -
    新增投资者-数量    float64    注意单位: 万户
    新增投资者-环比    float64    -
    新增投资者-同比    float64    -
    期末投资者-总量    float64    注意单位: 万户
    期末投资者-A股账户    float64    注意单位: 万户
    期末投资者-B股账户    float64    注意单位: 万户
    沪深总市值    float64    -
    沪深户均市值    float64    注意单位: 万
    上证指数-收盘    float64    -
    上证指数-涨跌幅    float64    -
    """
    account = ak.stock_account_statistics_em()
    account.set_index("数据日期", inplace=True)  # 设置索引值
    account.to_csv("I:\\bianchengxx\\pythonxx\\backtrader_001\\datas\\stock_account_statistics.csv")
    print(account)



if __name__ == '__main__':
    # sh_df()
    # sz_df()
    # sz_area()
    # sz_sector()
    # sh_day()
    get_account_statistics()
# 个股信息查询
# http://quote.eastmoney.com/concept/sh603777.html?from=classic

import akshare as ak

df = ak.stock_individual_info_em(symbol="002624")
print(df)
# 实时行情数据-东财
# 沪深京 A 股
# http://quote.eastmoney.com/center/gridlist.html#hs_a_board
import akshare as ak

# 实时行情数据-东财
# 沪深京 A 股
# 单次返回所有沪深京 A 股上市公司的实时行情数据
def em_spot():
    stock_zh_a_spot_em_df = ak.stock_zh_a_spot_em()
    print(stock_zh_a_spot_em_df)


# 实时行情数据-东财
# 沪 A 股
# http://quote.eastmoney.com/center/gridlist.html#sh_a_board
def em_sha_spot():
    stock_sh_a_spot_em_df = ak.stock_sh_a_spot_em()
    print(stock_sh_a_spot_em_df)


# 实时行情数据-东财
# 深 A 股
# http://quote.eastmoney.com/center/gridlist.html#sz_a_board
def em_sza_spot():
    stock_sz_a_spot_em_df = ak.stock_sz_a_spot_em()
    print(stock_sz_a_spot_em_df)


# 实时行情数据-东财
# 京 A 股
# http://quote.eastmoney.com/center/gridlist.html#bj_a_board
def em_bja_spot():
    stock_bj_a_spot_em_df = ak.stock_bj_a_spot_em()
    print(stock_bj_a_spot_em_df)


# 实时行情数据-东财
# 新股
# http://quote.eastmoney.com/center/gridlist.html#newshares
def em_new_spot():
    stock_new_a_spot_em_df = ak.stock_new_a_spot_em()
    print(stock_new_a_spot_em_df)


# 实时行情数据-东财
# 科创板
# http://quote.eastmoney.com/center/gridlist.html#hs_a_board
def em_kc_spot():
    stock_kc_a_spot_em_em_df = ak.stock_kc_a_spot_em()
    print(stock_kc_a_spot_em_em_df)


# 实时行情数据-新浪
# http://vip.stock.finance.sina.com.cn/mkt/#hs_a
def xl_a_spot():
    stock_zh_a_spot_df = ak.stock_zh_a_spot()
    print(stock_zh_a_spot_df)


if __name__ == "__main__":
    em_spot()
    # em_sha_spot()
    # em_sza_spot()
# 历史行情数据-东财
#  https://quote.eastmoney.com/concept/sh603777.html?from=classic
import akshare as ak
import pandas as pd

pd.set_option('expand_frame_repr', False)  # True就是可以换行显示。设置成False的时候不允许换行
pd.set_option('display.max_columns', None)  # 显示所有列
# pd.set_option('display.max_rows', None)  # 显示所有行
pd.set_option('colheader_justify', 'centre')  # 显示居中


def a_hist():
    # period
    #     str
    #     choice of {'daily', 'weekly', 'monthly'}
    # start_date
    #     str
    #     开始查询的日期
    # end_date
    #     str
    #     结束查询的日期
    # adjust
    #     str
    #     默认返回不复权的数据;
    #     qfq: 返回前复权后的数据;
    #     hfq: 返回后复权后的数据
    stock_zh_a_hist_df = ak.stock_zh_a_hist(symbol="002624", period="weekly", start_date="20190301", end_date='20230907',
                                            adjust="qfq")
    print(stock_zh_a_hist_df)



# 分时数据-东财
# http://quote.eastmoney.com/concept/sh603777.html?from=classic
# period    str     choice of {'1', '5', '15', '30', '60'}; 
# adjust    str     choice of {'', 'qfq', 'hfq'}; '': 不复权, 'qfq': 前复权, 'hfq': 后复权,
# 其中 1 分钟数据返回近 5 个交易日数据且不复权
def a_hist_min():
    stock_zh_a_hist_min_em_df = ak.stock_zh_a_hist_min_em(symbol="002624", start_date="2023-01-01 09:30:00", end_date="2023-02-03 15:00:00", period='30', adjust='')
    print(stock_zh_a_hist_min_em_df)


if __name__ == "__main__":
    a_hist()
    # a_hist_min()

4. 获取股票数据本地存储

import akshare as ak
import pandas as pd

pd.set_option('expand_frame_repr', False)  # True就是可以换行显示。设置成False的时候不允许换行
pd.set_option('display.max_columns', None)  # 显示所有列
pd.set_option('display.max_rows', None)  # 显示所有行
pd.set_option('colheader_justify', 'centre')  # 显示居中


def download_hist(symbol="002624", period="daily", start="1990101", end="20230318", adjust="qfq"):
    stock_zh_a_hist_df = ak.stock_zh_a_hist(symbol=symbol, period=period, start_date=start, end_date=end,
                                            adjust=adjust)
    stock_zh_a_hist_df.sort_values("日期", inplace=True)
    stock_zh_a_hist_df.set_index("日期", inplace=True)
    if period=="daily":
        stock_zh_a_hist_df.to_csv("I:\\akshare_stock\\stock_datas\\day\\{}.csv".format(symbol))
    elif period=="weekly":
        stock_zh_a_hist_df.to_csv("I:\\akshare_stock\\stock_datas\\week\\{}.csv".format(symbol))
    elif period=="monthly":
        stock_zh_a_hist_df.to_csv("I:\\akshare_stock\\stock_datas\\month\\{}.csv".format(symbol))
    else:
        print("period错误")


def download_hist_min(symbol="002624", start="1990-01-01 09:30:00", end="2023-02-03 15:00:00", period='30', adjust='qfq'):
    stock_zh_a_hist_min_em_df = ak.stock_zh_a_hist_min_em(symbol=symbol, start_date=start, end_date=end, period=period, adjust=adjust)
    stock_zh_a_hist_min_em_df.set_index("时间", inplace=True)
    stock_zh_a_hist_min_em_df.to_csv("I:\\akshare_stock\\stock_datas\\minute\\"+symbol+"_{}.csv".format(period))

if __name__ == "__main__":
    df = pd.read_csv("I:\\akshare_stock\\stock_datas\\stock_list.csv")
    df.sort_values("symbol",  inplace=True)
    code = list(df["ts_code"])
    print(len(code))
    for period in ['daily', 'weekly', 'monthly']:
        for i in range(0, len(code)):
            symbol =  code[i].rstrip('.SZHBJ')
            download_hist(symbol=symbol, period=period)
            print(symbol)

    for period in ['1', '5', '15', '30', '60']:
        for i in range(0, len(code)):
            symbol =  code[i].rstrip('.SZHBJ')
            download_hist_min(symbol=symbol, period=period)
            print(symbol)

5.将股票数据存到mysql数据库中

"""
date:20210918
将CSV文件写入到MySQL中
"""
import pandas as pd
from sqlalchemy import create_engine


def connect_db(db):
    engine = create_engine('mysql+pymysql://hao:671010@localhost:3306/{}?charset=utf8'.format(db))
    return engine


def create_stock(akcode, db, date):
    # 读取本地CSV文件
    df = pd.read_csv(
        'I:\\akshare_stock\\stock_datas\\' + date + '\\{}.csv'.format(akcode))
    engine = connect_db(db)
    # name='stocklist'全部小写否则会报错
    df.to_sql(name='ak_' + date + '_{}'.format(akcode), con=engine, index=False, if_exists='replace')

def create_min_stock(akcode, db, date):
    # 读取本地CSV文件
    df = pd.read_csv(
        'I:\\akshare_stock\\stock_datas\\minute\\'+akcode+'_{}.csv'.format(date))
    engine = connect_db(db)
    # name='stocklist'全部小写否则会报错
    df.to_sql(name='ak_' + akcode + '_{}'.format(date), con=engine, index=False, if_exists='replace')


def read_csv(code):
    df = pd.read_csv('I:\\akshare_stock\\stock_datas\\{}.csv'.format(code))
    return df


stockDB = 'akshare_stock'
stockList = 'stock_list'
create_stock(akcode=stockList, db=stockDB, date="day")
df1 = read_csv(stockList)
df1.sort_values("ts_code",  inplace=True)
li1 = list(df1['ts_code'])
for date in ["day", "week", "month"]:
    for i in range(0, len(li1)):
        codeStock = li1[i].rstrip('.SHZBJ')
        create_stock(akcode=codeStock, db=stockDB, date=date)
        print(codeStock)

for date in ['1', '5', '15', '30', '60']:
    for i in range(0, len(li1)):
        codeStock = li1[i].rstrip('.SHZBJ')
        create_min_stock(akcode=codeStock, db=stockDB, date=date)
        print(codeStock)

你可能感兴趣的:(python,数据分析,金融)