一,获取当天日期
import time
import os
import tushare as ts
import pandas as pd
date=time.localtime()
nowdate=time.strftime('%Y%m%d',date)
二、获取当天天数据
df = pro.daily(ts_code=i,trade_date=nowdate)
三、按版块写入之前文件
if market=='主板':
input_csv = open(os.path.join('F:\Data\ZhuBandata',i+'_data.csv'), 'a+')
df.to_csv(os.path.join('F:\Data\ZhuBandata',i+'_data.csv'), mode='a', header=False, index=None)
df=pd.read_csv(os.path.join('F:\Data\ZhuBandata',i+'_data.csv'))
df=df.sort_values('trade_date',ascending=True)
df=df.drop_duplicates()
df.to_csv(os.path.join('F:\Data\ZhuBandata',i+'_data.csv'),index=False)
input_csv.close()
四、全部代码 新手,代码冗余,待优化。
import time
import os
import tushare as ts
import pandas as pd
date=time.localtime()
nowdate=time.strftime('%Y%m%d',date)
pro=ts.pro_api('15692233a65a20bb1eaca7fb42a67a398c561575f2a2227abcd1eaa2')
data=pro.stock_basic(exchage='',list_status='L',
fields='ts_code,symbol,name,area,industry,market,list_date,is_hs')
mm=pd.Series(['主板','中小板','创业板','科创板','CDR'])
j=1
for k in mm:
data1=data[data.market==k]
data1.to_csv(os.path.join('F:\Data',k+'_company.csv'),index=False)
for i in data1.ts_code:
print(j,'股票代码:',i)
j+=1
df = pro.daily(ts_code=i,trade_date=nowdate)
market=list(data1['market'])
market=market[0]
df=df.sort_values('trade_date',ascending=True)
if market=='主板':
input_csv = open(os.path.join('F:\Data\ZhuBandata',i+'_data.csv'), 'a+')
df.to_csv(os.path.join('F:\Data\ZhuBandata',i+'_data.csv'), mode='a', header=False, index=None)
df=pd.read_csv(os.path.join('F:\Data\ZhuBandata',i+'_data.csv'))
df=df.sort_values('trade_date',ascending=True)
df=df.drop_duplicates()
df.to_csv(os.path.join('F:\Data\ZhuBandata',i+'_data.csv'),index=False)
input_csv.close()
elif market=='中小板':
input_csv = open(os.path.join('F:\Data\XiaoBandata',i+'_data.csv'), 'a+')
df.to_csv(os.path.join('F:\Data\XiaoBandata',i+'_data.csv'), mode='a', header=False, index=None)
df=pd.read_csv(os.path.join('F:\Data\XiaoBandata',i+'_data.csv'))
df=df.sort_values('trade_date',ascending=True)
df=df.drop_duplicates()
df.to_csv(os.path.join('F:\Data\XiaoBandata',i+'_data.csv'),index=False)
input_csv.close()
elif market=='创业板':
input_csv = open(os.path.join('F:\Data\CYeBandata',i+'_data.csv'), 'a+')
df.to_csv(os.path.join('F:\Data\CYeBandata',i+'_data.csv'), mode='a', header=False, index=None)
df=pd.read_csv(os.path.join('F:\Data\CYeBandata',i+'_data.csv'))
df=df.sort_values('trade_date',ascending=True)
df=df.drop_duplicates()
df.to_csv(os.path.join('F:\Data\CYeBandata',i+'_data.csv'),index=False)
input_csv.close()
elif market=='科创板':
input_csv = open(os.path.join('F:\Data\KCBandata',i+'_data.csv'), 'a+')
df.to_csv(os.path.join('F:\Data\KCBandata',i+'_data.csv'), mode='a', header=False, index=None)
df=pd.read_csv(os.path.join('F:\Data\KCBandata',i+'_data.csv'))
df=df.sort_values('trade_date',ascending=True)
df=df.drop_duplicates()
df.to_csv(os.path.join('F:\Data\KCBandata',i+'_data.csv'),index=False)
input_csv.close()
elif market=='CDR':
input_csv = open(os.path.join('F:\Data\CDRdata',i+'_data.csv'), 'a+')
df.to_csv(os.path.join('F:\Data\CDRdata',i+'_data.csv'), mode='a', header=False, index=None)
df=pd.read_csv(os.path.join('F:\Data\CDRdata',i+'_data.csv'))
df=df.sort_values('trade_date',ascending=True)
df=df.drop_duplicates()
df.to_csv(os.path.join('F:\Data\CDRdata',i+'_data.csv'),index=False)
input_csv.close()
print('获取到'+k+'股票:',len(data1))
|