akshare文档,这个akshare目前看比baostock更全,baostock没有etf数据,这边的文档也写的更清晰,可以参考着学一下
AKShare 股票数据 — AKShare 1.9.10 文档
1、行业成交情况
import akshare as ak
# 查看当年当月的各行业成交情况,一般当年没有结束的情况下,不能选当年,只能选当月
stock_szse_sector_summary_df = ak.stock_szse_sector_summary(symbol="当月", date="202302")
print(stock_szse_sector_summary_df)
import akshare as ak
# 地区交易排序
stock_szse_area_summary_df = ak.stock_szse_area_summary(date="202203")
print(stock_szse_area_summary_df)
市场全貌
import akshare as ak
# 上证全貌
stock_sse_summary_df = ak.stock_sse_summary()
# 深证原貌
stock_szse_summary_df = ak.stock_szse_summary(date="20200619")
print("上证:")
print(stock_sse_summary_df)
print("深证:")
print(stock_szse_summary_df)
import akshare as ak
# 查看股票的基本概况
stock_individual_info_em_df = ak.stock_individual_info_em(symbol="000001")
print(stock_individual_info_em_df)
不知道为啥运行drop_duplicateds()会报错,说是不mutable的不懂
# 通过ak.fund_etf_category_sina()接口获取ETF列表,代码如下:
# 获取etf的历史数据
import pandas as pd
import akshare as ak
code = pd.read_csv("\\.sina_etf_list.csv")
code = code['代码'].values.tolist()
lst = []
for etf_code in code:
df = ak.fund_etf_hist_sina(symbol=etf_code)
df['code'] = etf_code
df['date']=pd.to_datetime(df['date'])
df.set_index('date',inplace=True)
df = df.loc['2022/01/01' : '2023/03/13']
lst.append(df)
final_data = pd.DataFrame(lst)
# final_data.drop_duplicates()
final_data.to_csv('\\etf-data.csv',encoding = 'utf-8')
print('finished')
用这个更合适,这个是前复权的数据
import akshare as ak
fund_etf_hist_em_df = ak.fund_etf_hist_em(symbol="513500", period="daily", start_date="20000101", end_date="20230201", adjust="qfq")
print(fund_etf_hist_em_df)
用这个更适合
AKShare 公募基金数据 — AKShare 1.9.10 文档
关于extend,直接把二维列表变成一维列表;
更改列的位置,注意一下,这里面为啥不能直接 用空的dataframe添加,我也不太明白,之后媒体那跑这个就行,只需要改一下字典中的数据
# 通过ak.fund_etf_category_sina()接口获取ETF列表,代码如下:
# 获取etf的历史数据
import pandas as pd
import akshare as ak
import datetime
import csv
fp = open(r'D:\test\daily_data.csv','a',newline = '')
etf = {'159740':'abv'}
dic = {"512660":"军工ETF",
"515030":"新能源车ETF",
"516150":"稀土ETF基金",
"516160":"新能源ETF",
"159755":"电池ETF",
"159865":"养殖ETF",
"159780":"双创ETF"}
stock = {'601688':'华泰证券','002100':'天康生物','601878':'浙商证券'}
# 获取日期
start_date=datetime.datetime.now()+datetime.timedelta(days=-1)
end_date=datetime.datetime.now()+datetime.timedelta(days=-1)
start = start_date.strftime('%Y%m%d')
end =end_date.strftime('%Y%m%d')
# lst = pd.DataFrame(data = None,columns = ['日期','开盘','收盘','最高','最低','成交量','成交额','振幅','涨跌幅','涨跌额','换手率','stock_code','code_name'])
lst = [['日期', 'stock_code', 'code_name','开盘', '收盘', '最高', '最低', '成交量', '成交额', '振幅', '涨跌幅', '涨跌额', '换手率']]
for code,code_name in stock.items():
df = ak.stock_zh_a_hist(symbol=code, period="daily",
start_date=start, end_date=end,
adjust="qfq")
df['stock_code']=code
df['code_name']= code_name
# 更改列的顺序,把姓名排在前面
order = ['日期', 'stock_code', 'code_name','开盘', '收盘', '最高', '最低', '成交量', '成交额', '振幅', '涨跌幅', '涨跌额', '换手率']
df = df[order]
# print(df)
df = df.values.tolist()
lst.extend(df)
for code,code_name in dic.items():
df1 = ak.fund_etf_hist_em(symbol= code, period="daily",
start_date=start, end_date=end,
adjust="qfq")
df1['stock_code'] = code
df1['code_name']= code_name
order = ['日期', 'stock_code', 'code_name','开盘', '收盘', '最高', '最低', '成交量', '成交额', '振幅', '涨跌幅', '涨跌额', '换手率']
df1 = df1[order]
df1 = df1.values.tolist()
# extend可以把二维列表扩展成为一维列表,整个dataframe对象直接就是一个二维列表,
# 所以这里用append不对
lst.extend(df1)
# # lst
writer = csv.writer(fp)
writer.writerows(lst)
fp.close()
print('finished!')