您的位置:首页 > 理论基础 > 计算机网络

Matlab----网络爬虫例子

2016-04-22 07:10 766 查看
% 本程序用于获取网站中的表格
% written by longwen36
% all rights reserved
clc,clear;
warning off;

for year = 1991:1992 %年份
for jidu = 1:4
fprintf('%d年%d季度的数据...', year, jidu)
[sourcefile, status] = urlread(sprintf('http://vip.stock.finance.sina.com.cn/corp/go.php/vMS_MarketHistory/stockid/000001/type/S.phtml?year=%d&jidu=%d', year,jidu));
if ~status
error('读取出错!\n')
end
expr1 = '\s+(\d\d\d\d-\d\d-\d\d)\s*'; %获取日期
[datefile, date_tokens]= regexp(sourcefile, expr1, 'match', 'tokens');
date = cell(size(date_tokens));
for idx = 1:length(date_tokens)
date{idx} = date_tokens{idx}{1};
end

expr2 = '<div align="center">(\d*\.?\d*)</div>'; %获取数据

[datafile, data_tokens] = regexp(sourcefile, expr2, 'match', 'tokens');

data = zeros(size(data_tokens));

for idx = 1:length(data_tokens)
data(idx) = str2double(data_tokens{idx}{1});
end
data = reshape(data, 6, length(data)/6 )'; %重排
filename = sprintf('%d年',year);
pathname = [pwd '\data'];
if ~exist(pathname,'dir')
mkdir(pathname);
end
fullfilepath = [pwd '\data\' filename];
% 保存数据到Excel
sheet = sprintf('第%d季度', jidu);
xlswrite(fullfilepath, date' , sheet);
range = sprintf('B1:%s%d',char(double('B')+size(data,2)-1), size(data,1));
xlswrite(fullfilepath, data, sheet, range);
fprintf('OK!\n')
end
end

fprintf('全部完成!\n')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: