%
stockget.m%该代码实现了从新浪提供的股票数据网站上实时抓取数据并且的功能%%抓取网页信息clcStockCode=300191;%股票代码if(StockCode==999999)symbol=strcat('sh000001');elseif(StockCode>=600000)symbol=strcat('sh',num2str(StockCode));elsesymbol=strcat('sz',num2str(StockCode));endurl2Read=['http://hq.sinajs.cn/list=',symbol];%新浪提供的股票数据地址,获取网页信息s=urlread(url2Read);result=textscan(s,'%s','delimiter',',');result=result{1};%%Name=cell2mat(result(1));%名称Name=Name(22:end);date=result{31};OpenPrice=str2double(result{2});%今日开盘价;time=result{32};TPrice=str2double(result{4});%当前价格;HighPrice=str2double(result{5});%当前最高价格;LowPrice=str2double(result{6});%当前最低价格Vol=str2double(result{9})/100;%成交量disp(['股票名称:',Name,'股票代码:',symbol,'日期:',date]);disp(['开盘价:',result{2}]);disp(['当前时间:',time]);disp(['当前价格:',result{4}]);disp(['当前最高价格:',result{5},'当前最低价格:',result{6}]);disp(['成交量:',num2str(Vol)]);disp(['买1数量:',result{11},'买1价格:',result{12},'买2数量:',result{13},'买2价格:',result{14},'买3数量:',result{15},'买3价格:',result{16},'买4数量:',result{17},'买4价格:',result{18},'买5数量:',result{19},'买5价格:',result{20}]);disp(['卖1数量:',result{21},'卖1价格:',result{22},'卖2数量:',result{23},'卖2价格:',result{24},'卖3数量:',result{25},'卖3价格:',result{26},'卖4数量:',result{27},'卖4价格:',result{28},'卖5数量:',result{29},'卖5价格:',result{30}]);%处理时间信息time([3,6])=[];timehour=str2double(time(1:2));timeminute=str2double(time(3:4));timesecond=str2double(time(5:6));timereal=timehour+timeminute/60+timesecond/3600;%xlswrite('gupiao.xls',[timereal,TPrice],'');%%%写入文件%写入文档中记录实时的股票数据',num2str(TPrice),...'',num2str(Vol),'',result{11},'',...result{12},'',result{13},'',result{14},...'',result{15},'',result{16},'',result{17},...datawrite=[num2str(timereal),''',result{18},'',result{19},'',result{20},...'',result{21},'',result{22},'',result{23},...'',result{24},'',result{25},'',result{26},...'',result{27},'',result{28},'',result{29},'',result{30}];filenote=['股票',date,'.txt'];fid=fopen(filenote,'at');%data_all_1=num2str(importdata(filenote));%data_all_2=reshape(data_all_1,1,[]);%bull=strfind(data_all_2,datawrite);%%%if(bull==0)fprintf(fid,'%s\\n',datawrite);endfprintf(fid,'%s\\n',datawrite);%fprintf(fid,'%s\\n',datawrite);%%%作图fclose(fid);data_all=importdata(filenote);timenow=data_all(:,1);pricenow=data_all(:,2);chengjiaoliang=data_all(:,3);buynum=data_all(:,[4,6,8,10,12]);buyprice=data_all(:,[5,7,9,11,13]);sellnum=data_all(:,[14,16,18,20,22]);sellprice=data_all(:,[15,17,19,21,23]);subplot(2,1,1);%做出时间价格曲线xlabel('时间');ylabel('价格');plot(timenow,pricenow)gridon;subplot(2,1,2);%做出时间成交量曲线xlabel('时间');ylabel('成交量');gridon;=get(gca,''CurrentPoint'');text(pt(1,1),pt(1,2),num2str(pt(1,1:2)))');%%%%plot(timenow,buynum(:,1),'b',timenow,buynum(:,2),'r',timenow,buynum(:,3),'k',timenow,buynum(:,4),'y',timenow,buynum(:,5),'g');%title('买1到买5量变化');figure(2);subplot(2,2,1);%set(gcf,'WindowButtonDownFcn','ptplot(timenow,chengjiaoliang)%%%%2');gridon;legend('买1','买2','买3','买4','买subplot(2,2,2);plot(timenow,buyprice(:,1),'b',timenow,buyprice(:,2),'r',timenow,buyprice(:,3),'k',timenow,buyprice(:,4),'y',timenow,buyprice(:,5),'g');%%%%plot(timenow,sellnum(:,1),'b',timenow,sellnum(:,2),'r',timenow,sellnum(:,3),'k',timenow,sellnum(:,4),'y',timenow,sellnum(:,5),'g');%%%title('卖1到卖5量变化');gridon;subplot(2,2,4);title('买1到买5价格变化');gridon;subplot(2,2,3);%plot(timenow,sellprice(:,1),'b',timenow,sellprice(:,2),'r',timenow,sellprice(:,3),'k',timenow,sellprice(:,4),'y',timenow,sellprice(:,5),'g');%%title('卖1到卖5价格变化');gridon;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%为了能定时运行上述程序需要用到计时器,每5秒运行一次stockget.mx1=1;tt=timer('TimerFcn','stockget','Period',5,'ExecutionMode','fixedSpacing','TasksToExecute',100000);start(tt);localtime=clock;