import baostock as bs
import pandas as pd
import time
st = time.time()
# 登陆系统
lg = bs.login()
# 读取所有的股票代码
df = pd.read_csv(r'D:\test\all_stock.csv',encoding = 'gbk')
code_list = df['code'].values.tolist()
# 获取天数
start_date='2022-01-01'
end_date='2023-03-08'
start = datetime.datetime.strptime(start_date,'%Y-%m-%d')
end = datetime.datetime.strptime(end_date,'%Y-%m-%d')
s = (end-start).days
# 存储list
data_list = []
# 获取沪深A股历史K线数据
for code in code_list:
if len(code)!=9:
continue
rs_result = bs.query_history_k_data_plus(code,
fields="date,code,open,high,low,close,preclose,volume,amount,adjustflag,turn,tradestatus,pctChg,isST",
start_date='2022-01-01',
end_date='2023-03-08',
frequency="d",
adjustflag="3")
# print(rs_result.get_data())
i = 0
while (i<s) &(rs_result.error_code == '0')& rs_result.next():
data_list.append(rs_result.get_row_data())
i+=1
# 把结果转为DataFrame
result = pd.DataFrame(data_list,columns=rs_result.fields)
#### 结果集输出到csv文件 ####
result.to_csv("D:\\history_A_stock_k_data_all1.csv", index=False)
# print(result)
#### 登出系统 ####
bs.logout()
ed = time.time()
run = ed-st
print('运行的时间为%ds'%(run))
print('finished!')
主要是怎么合并两个dataframe,以及用pivot_table 和用groupby怎么操作,以及用concat和merge
import pandas as pd
import numpy as np
df = pd.read_csv("D:\\history_A_stock_k_data_all1.csv",encoding = 'utf=8')
df2 = pd.read_csv(r'D:\stock_industry.csv',encoding = 'gbk')
df7 = pd.read_csv(r'D:\test\all_stock.csv',encoding = 'gbk')
# df1 = pd.DataFrame()
# mean1 = df[['code','close']].groupby('code').mean()
# mean.values.tolist()
# df1['max'] = df[['code','close']].groupby('code').max()
# min1= df[['code','close']].groupby('code').min()
# df1['lst_pr'] = df[(df['date']=='2023-3-8')]
# df2 = pd.concat([mean1,min1])
# df2.append([max1,min1,lst_pr])
# df2 = df.pivot_table(index =['code'],values = ['close'],aggfunc = ['max', 'mean','min'])
# df = df2.loc[3::]
df1 = df[(df['date']=='2023-03-08')][['code','close']]
# df2.to_csv("D:\\history_A_stock_k_data_count.csv")
# df2.index.name = 'code'
# df2.iloc[1::]
df3 = df[['code','close']].groupby('code').agg([np.mean,np.min,np.max])
# df = df3.get_group('code').reset_index()
df3 = df3.reset_index()
# df2 = df[[code]]
df4 = pd.merge(df3,df1,how = 'left',on = 'code')
df5 = pd.merge(df4,df2,how ='left',on = 'code')
df6 = pd.merge(df5,df7,how ='left',on = 'code')
# df3['code']
# df1
df6.to_csv(r'D:\\history_A_stock_k_data_12.csv',encoding ='gbk',index = False)
每天跑一跑,但是发现这里面没有etf的数据,听说还有一个库akshare也可以, 明天我看看怎么操作的
import baostock as bs
import pandas as pd
import time
import datetime
st = time.time()
# 登陆系统
lg = bs.login()
# 读取所有的股票代码
df = pd.read_csv(r'D:\test\all_stock.csv',encoding = 'gbk')
# code_list = df['code'].values.tolist()
code_list = ['sh.601688','sz.002100','sh.601878']
# print()
# 获取天数
start_date='2023-03-10'
end_date='2023-03-13'
start = datetime.datetime.strptime(start_date,'%Y-%m-%d')
end = datetime.datetime.strptime(end_date,'%Y-%m-%d')
s = (end-start).days
# 存储list
data_list = []
# 获取沪深A股历史K线数据
for code in code_list:
if len(code)!=9:
continue
rs_result = bs.query_history_k_data_plus(code,
fields="date,code,open,high,low,close,preclose,volume,amount,adjustflag,turn,tradestatus,pctChg,isST",
start_date='2023-03-13',
end_date='2023-03-13',
frequency="d",
adjustflag="3")
# print(rs_result.get_data())
i = 0
while (i<s) &(rs_result.error_code == '0')& rs_result.next():
data_list.append(rs_result.get_row_data())
i+=1
# 把结果转为DataFrame
result = pd.DataFrame(data_list,columns=rs_result.fields)
### 结果集输出到csv文件 ####
result.to_csv("D:\\history_A_stock_k_data_gupiao.csv", index=False)
# print(result)
#### 登出系统 ####
bs.logout()
ed = time.time()
run = ed-st
print('运行的时间为%ds'%(run))
print('finished!')
修改了一下,每天跑这个就行了,这个好像每天六点左右才会有当天的数据
import baostock as bs
import pandas as pd
import time
import datetime
st = time.time()
# 登陆系统
lg = bs.login()
# 获取某几个股票每天的日线数据
code_list = ['sh.601688','sz.002100','sh.601878']
# 获取日期,获取前一天的日期,用datetime.timedelta
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')
# 存储list
s = 1
data_list = []
# 获取沪深A股历史K线数据
for code in code_list:
if len(code)!=9:
continue
rs_result = bs.query_history_k_data_plus(code,
fields="date,code,open,high,low,close,preclose,volume,amount,adjustflag,turn,tradestatus,pctChg,isST",
start_date=start,
end_date=end,
frequency="d",
adjustflag="3")
# print(rs_result.get_data())
i = 0
while (i<s) & (rs_result.error_code == '0')& rs_result.next():
data_list.append(rs_result.get_row_data())
i+=1
# 把结果转为DataFrame
result = pd.DataFrame(data_list,columns=rs_result.fields)
### 结果集输出到csv文件 ####
result.to_csv("D:\\history_A_stock_k_data_gupiao.csv", index=False)
# print(result)
#### 登出系统 ####
bs.logout()
ed = time.time()
run = ed-st
print('运行的时间为%ds'%(run))
print('finished!')