From e837767bb5ab3e2c123fdab73c0791b0e503c77b Mon Sep 17 00:00:00 2001 From: aszerW Date: Sun, 12 Oct 2025 13:35:30 +0800 Subject: [PATCH] =?UTF-8?q?A=E8=82=A1=E6=89=80=E6=9C=89=E6=8C=87=E6=95=B0?= =?UTF-8?q?=E6=9C=80=E6=96=B0=E4=BB=B7=E6=A0=BC=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index_downloader.py | 51 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 index_downloader.py diff --git a/index_downloader.py b/index_downloader.py new file mode 100644 index 0000000..d6c523f --- /dev/null +++ b/index_downloader.py @@ -0,0 +1,51 @@ +import os + +import akshare as ak +import pandas as pd +from loguru import logger + +# index_hist_df = ak.index_zh_a_hist( +# symbol="000001", # 指数代码,如上证指数 +# period="daily", # K线周期: daily(日K) +# start_date="19700101", # 开始日期 +# end_date="22220101", # 结束日期 +# ) + + +def get_all_stock_index(): + index_choice = ["沪深重要指数", "上证系列指数", "深证系列指数", "中证系列指数"] + index_df_list = [] + for source in index_choice: + logger.info(f"正在获取 {source}...") + index_df = ak.stock_zh_index_spot_em(symbol=source) + index_df["symbol"] = source + index_df_list.append(index_df) + logger.info(f"{source}: {index_df.shape[0]}") + df = pd.concat(index_df_list) + return df + + +if __name__ == "__main__": + # df = get_all_stock_index() + # df.to_csv("index_all_stock.csv", index=False, encoding="utf-8-sig") + + df = pd.read_csv("index_all_stock.csv", encoding="utf-8-sig") + for index, row in df.iterrows(): + print(f"正在获取 {index} {row['代码']} ...") + file_path = f"data/{row['代码']}.csv" + if os.path.exists(file_path): + print(f"{row['代码']} 存在") + continue + index_hist_df = ak.index_zh_a_hist( + symbol=row["代码"], # 指数代码,如上证指数 + period="daily", # K线周期: daily(日K) + start_date="19700101", # 开始日期 + end_date="22220101", # 结束日期 + ) + index_hist_df.to_csv( + f"data/{row['代码']}.csv", index=False, encoding="utf-8-sig" + ) + # time.sleep(15) + # print(index_hist_df) + + ...