使用Bastock获取从开始日期到结束日期的sse50重量级信息,并将其保存到CSV文件中,Baostock,起始,终止,SSE50,权重股,至

发表时间:2020-11-16
import baostock as bs
import pandas as pd
import time
import datetime

#  获取时间 从start_date
def get_date(start_date, end_date):
    # start_date = "2010-01-01"
    # end_date = "2020-10-01"
    date_list = []
    start_date_stc = datetime.datetime.strptime(start_date, '%Y-%m-%d')
    end_data_stc = datetime.datetime.strptime(end_date, '%Y-%m-%d')
    delta = datetime.timedelta(days=7)
    flag = True
    end_date_time_array = time.strptime(end_date, '%Y-%m-%d')
    end_date_timestamp = time.mktime(end_date_time_array)
    while (flag):
        date = start_date_stc.strftime('%Y-%m-%d')
        start_date_stc = start_date_stc + delta
        start_date = start_date_stc.strftime('%Y-%m-%d')
        start_date_time_array = time.strptime(start_date, '%Y-%m-%d')
        start_date_time_timestamp = time.mktime(start_date_time_array)
        if start_date_time_timestamp >= end_date_timestamp:
            flag = False
        date_list.append(date)
    return date_list

#

if __name__ == "__main__":
    # 登录baostock网站并且返回登录信息
    lg = bs.login()
    print('login respond error_code:' + lg.error_code)
    print('login respond  error_msg:' + lg.error_msg)
    date_list = get_date(start_date="2010-01-01", end_date="2020-10-01")
    sz50_stocks = []
    for date in  date_list:
        rs = bs.query_sz50_stocks(date = date)
        while (rs.error_code == '0') & rs.next():
            # 获取一条记录,将记录合并在一起
            sz50_stocks.append(rs.get_row_data())

    result = pd.DataFrame(sz50_stocks, columns=['updateDate', 'code', 'code_name'])
    result.to_csv("sz50_stocks.csv", encoding="gbk", index=False)

文章来源互联网,如有侵权,请联系管理员删除。邮箱:417803890@qq.com / QQ:417803890

微配音

Python Free

邮箱:417803890@qq.com
QQ:417803890

皖ICP备19001818号
© 2019 copyright www.pythonf.cn - All rights reserved

微信扫一扫关注公众号:

联系方式

Python Free