MATLAB 爬取网络数据
by HPC_ZY
第一步:申请数据
以优酷为例
url='https://www.youku.com/'; % 数据所在网址重点内容
sourcefile=webread(url); % 获取所有数据
第二步:观察并简化数据
- 显示数据,以观察所需信息的结构组成
disp(sourcefile) % 显示数据
- 如想获取剧集、电影等名,则找到其结构组成
- 为方便查找,可去除空白字符(根据个人具体需要,并不是所有情况的都适合)
sourcefile(isspace(sourcefile))=[]; % 去除空白字符(选用)
第三步:提取信息
输入对应的“正则表达式“进行检索
expr='<atitle="(\w+)"href="';
[~, data]= regexp(sourcefile, expr, 'match', 'tokens'); % 检索
celldisp(data) % 显示结果
结果以cell格式存储,部分结果如下:
注:关于正则表达式以及regexp()函数的使用方法,可通过以下方式查询
MATLAB官网:https://ww2.mathworks.cn/help/matlab/ref/regexp.html?searchHighlight=regexp&s_tid=doc_srchtitle
MATLAB命令行窗口:help regexp
附:完整代码
% 第一步 获取数据
url='https://www.youku.com/';
sourcefile=webread(url); % 获取
% 第二步 观察并整理数据
disp(sourcefile); % 显示数据
sourcefile(isspace(sourcefile))=[]; % 去除空白字符(根据个人需要选用)
% 第三步 提取信息
expr='<atitle="(\w+)"href="'; % (正则表达式)输入你想查询的内容
[~, data]= regexp(sourcefile, expr, 'match', 'tokens'); % 检索
celldisp(data) % 显示信息