代码如下:
function stockinfo = queryprice(stocktype, stockid) %stocktype 股票类型:sh和sz %stockid 股票编码: url = sprintf('http://hq.sinajs.cn/list=%s%d', stocktype, stockid); [sourcefile, status] = urlread(url); if (~status) disp('无法获取数据'); else %取出字符串 str = javaObject('java.lang.String', sourcefile); str = str.replaceAll('var ', ''); strs = str.split('='); str = strs(2); str = str.replaceFirst('"', '{'''); str = str.replaceFirst('"', '''};'); str = str.replaceAll(',', ''', '''); str = strcat('stock=', char(str)); eval(str); stockinfo = {}; stockinfo.name = stock(1); stockinfo.today_start_price = str2double(stock(2)); stockinfo.yestoday_end_price = str2double(stock(3)); stockinfo.current_price = str2double(stock(4)); stockinfo.today_highest_price = str2double(stock(5)); stockinfo.today_lowest_price = str2double(stock(6)); stockinfo.today_first_buy_price = str2double(stock(7)); stockinfo.today_first_sale_price = str2double(stock(8)); stockinfo.today_success_stock_mount = str2double(stock(9))/100; %单位:手 stockinfo.today_success_money = str2double(stock(10)); %单位:万 stockinfo.today_first_buy_price = str2double(stock(11)); stockinfo.today_first_sale_price = str2double(stock(12)); stockinfo.buy_stock_mount1 = str2double(stock(13)) / 100; %单位:手 stockinfo.buy_stock_price1 = str2double(stock(14)); %单位:万 stockinfo.buy_stock_mount2 = str2double(stock(15)); stockinfo.buy_stock_price2 = str2double(stock(16)); stockinfo.buy_stock_mount3 = str2double(stock(17)); stockinfo.buy_stock_price3 = str2double(stock(18)); stockinfo.buy_stock_mount4 = str2double(stock(19)); stockinfo.buy_stock_price4 = str2double(stock(20)); stockinfo.buy_stock_mount5 = str2double(stock(21)); stockinfo.buy_stock_price5 = str2double(stock(22)); stockinfo.sale_stock_mount1 = str2double(stock(23)); stockinfo.sale_stock_price1 = str2double(stock(24)); stockinfo.sale_stock_mount2 = str2double(stock(25)); stockinfo.sale_stock_price2 = str2double(stock(26)); stockinfo.sale_stock_mount3 = str2double(stock(27)); stockinfo.sale_stock_price3 = str2double(stock(28)); stockinfo.sale_stock_mount4 = str2double(stock(29)); stockinfo.sale_stock_price4 = str2double(stock(30)); stockinfo.date = stock(31); stockinfo.time = stock(32); end end
示例使用:
info = queryprice(‘sh‘, 601006) %大秦铁路
结果为:
info =
name: {‘大秦铁路‘}
today_start_price: 10.5800
yestoday_end_price: 10.6300
current_price: 10.8500
today_highest_price: 10.9500
today_lowest_price: 10.5000
today_first_buy_price: 272852
today_first_sale_price: 10.8500
today_success_stock_mount: 1.1826e+06
today_success_money: 1.2708e+09
buy_stock_mount1: 410
buy_stock_price1: 10.8400
buy_stock_mount2: 43000
buy_stock_price2: 10.8300
buy_stock_mount3: 61300
buy_stock_price3: 10.8200
buy_stock_mount4: 67700
buy_stock_price4: 10.8100
buy_stock_mount5: 109038
buy_stock_price5: 10.8600
sale_stock_mount1: 86015
sale_stock_price1: 10.8700
sale_stock_mount2: 273900
sale_stock_price2: 10.8800
sale_stock_mount3: 180878
sale_stock_price3: 10.8900
sale_stock_mount4: 313838
sale_stock_price4: 10.9000
date: {‘2014-12-26‘}
time: {‘15:03:06‘}