沪深龙虎榜数据进一步处理,计算日后5日的涨跌幅
事前数据:
前面处理得到的csv文件
文件名前加入“[wait]”等待程序处理
python代码从雅虎股票历史数据api获取数据,计算后面5日的涨跌幅
雅虎数据api格式:
PRiceUrl = 'http://table.finance.yahoo.com/table.csv?s={%1}&d={%2}&e={%3}&f={%4}&g=d&a={%5}&b={%6}&c={%7}&ignore=.csv'
# %1:000001.sz
# END: %2:月-1 %3:日 %4:年
# STRAT: %5:月-1 %6:日 %7:年
事前数据截图:
计算后,再用Excel的条件格式并另存为excel文件后截图:
代码:
1#coding=utf-823#读取'[wait]'开头的csv文件4#copyright @ WangXinsheng5#http://www.cnblogs.com/wangxinsheng/6importos7importgzip8importre9importhttp.cookiejar10importurllib.request11importurllib.parse12importtime13importdatetime1415defgetOpener(head):16#deal with the Cookies17cj =http.cookiejar.CookieJar()18pro =urllib.request.HTTPCookieProcessor(cj)19opener =urllib.request.build_opener(pro)20header =[]21forkey, valueinhead.items():22elem =(key, value)23header.append(elem)24opener.addheaders =header25returnopener2627defungzip(data):28try:#尝试解压29print('正在解压.....')30data =gzip.decompress(data)31print('解压完毕!')32except:33print('未经压缩, 无需解压')34returndata3536#常量37header ={38#'Connection': 'Keep-Alive',39'Accept':'*/*',40'Accept-Language':'zh-CN,zh;q=0.8',41'User-Agent':'Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/40.0.2214.111 Safari/537.36',42'Accept-Encoding':'gzip, deflate',43'Host':'yahoo.com',44'Referer':'http://www.yahoo.com'45}46priceUrl ='http://table.finance.yahoo.com/table.csv?\47s={%1}&d={%2}&e={%3}\48&f={%4}&g=d&a={%5}&b={%6}&c={%7}&ignore=.csv'49#%1:000001.sz50#END: %2:月-1 %3:日-1 %4:年51#STRAT: %5:月-1 %6:日 %7:年525354path=r'.'55files =os.listdir(path)56files.sort()57out=[]5859forfinfiles:60if(f.startswith('[wait]')and61f.endswith('.csv')):62#读取文件63print('读取文件:'+path+'/'+f)6465f=open(path+'/'+f,'rt')66infos =f.readlines()67f.close()6869i =070add =False71forinfoininfos:72if(i==0):73i=i+174info = info.replace('\n','')+',"一天后","二天后","三天后","四天后","五天后"\n'75out.append(info)76continue77elif(len(info.split(','))>9):78out.append(info)79continue80else:81#确认需要取的数据范围82tmp = info.split(',')83try:84timeArray = time.strptime(tmp[0],"%Y-%m-%d")85except:86timeArray = time.strptime(tmp[0],"%Y/%m/%d")87timeStamp =int(time.mktime(timeArray))88fromDay =datetime.datetime.utcfromtimestamp(timeStamp)89fromDay = fromDay + datetime.timedelta(days = 1)90endDay = fromDay + datetime.timedelta(days = 15)91code = tmp[1].replace('"','').replace("'","")92if(code.startswith('6')):93code = code +'.ss'94else:95code = code +'.sz'96url = priceUrl.replace('{%1}',code).replace('{%2}',str(endDay.month-1))97url = url.replace('{%3}',str(endDay.day)).replace('{%4}',str(endDay.year))98url = url.replace('{%5}',str(fromDay.month-1)).replace('{%6}',str(fromDay.day))99url = url.replace('{%7}',str(fromDay.year))100print('抓取URL:'+url)101102#通过雅虎获取价格103dd =''104try:105opener =getOpener(header)106op =opener.open(url)107data =op.read()108data =ungzip(data)109dd =data.decode()110except:111print('网络抓取失败')112out.append(info)113continue114#计算涨跌幅百分比115if(dd!=''):116dataInfo = dd.split('\n')117j=0118dayCount =0119startPrice =0120forxinrange(len(dataInfo)-1,0,-1):121#处理数据122if(dataInfo[x]==''):123continue124#print(dataInfo[x])125if(dayCount>5):126
break127di =dataInfo[x]128if(dayCount==0):129startPrice = float(di.split(',')[4])130elif(int(di.split(',')[5])!=0):131add =True132closeP = float(di.split(',')[4])133info = info.replace('\n','')+',"'+str(round((closeP-startPrice)/startPrice*100,2))+'%['+str(closeP)+']"'134#print(info)135if(dayCount==0orint(di.split(',')[5])!=0):136dayCount=dayCount+1137138if(add):139out.append(info+'\n')140#print(out)141continue142#输出143ff = open(path+'/'+f.name.replace('[wait]','[处理完了]'),'w')144foroinout:145ff.write(o)146ff.close()147print('处理完了\n文件地址:'+path+'/'+f.name.replace('[wait]','[处理完了]'))148else:149continue