Python uses akshare to crawl financial data in batches and save them in excel format

Python uses akshare to crawl data in batches and save it in excel format

The crawled website is http://vip.stock.finance.sina.com.cn/mkt/#hs_a's historical data.
First, make sure you have downloaded the third-party module akshare
download code:

pip install akshare -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host=mirrors.aliyun.com  --upgrade

Then, create a txt file named aa, the file content is
sh603329
sh603330
sh603331
sh603332
sh603333
sh603335
sh603336
sh603337
sh603338
sh603339
sh603345
sh603348
sh603351
sh603353
sh603355
sh603356
sh603357
sh603358
sh603359
sh603360
sh603363
sh603365
sh603366

Then you can run the python code! (These are all historical data)

import time
import akshare as ak
import pandas as pd

with open("aa.txt","r") as fp:
    fp.readline()
    for i in fp:
        b = i.split()
        stock_zh_a_daily_hfq_df = ak.stock_zh_a_daily(symbol=b[0], adjust="hfq")
        a = stock_zh_a_daily_hfq_df

        # 定义文件
        file_name=b[0]+".xlsx"

        writer = pd.ExcelWriter(file_name, encoding="utf-8-sig")
        a.to_excel(writer, "sheet1")
        writer.save()
        print("数据保存成功")

Guess you like

Origin blog.csdn.net/m0_50481455/article/details/109111015