主要内容
偶然在网上看到matlab也可以写爬虫,而且流程很简单,记录一下学习过程和其中遇到的问题。主要包括简单爬虫的流程(2种方法),所用函数,weboptions设置(Timeout默认5s太短),正则表达式匹配简单提升效率的一个小的注意事项.
爬虫流程方法1
用actxserver(‘internetexplorer.application’)建立与ie浏览器的交互,到爬取网页源代码里找到所需信息的classname(F12加审查元素选取找到classname),之后提取class中的值获的想要爬取的信息。

这个方法是我最先看到的一种方法,但是不知道为什么actxserver这里我就会直接报错,所以没有成功…,想要具体学习这种方法建议直接去搜索更详细的教程。
ie=actxserver('internetexplorer.application');
ie.Navigate('网址');%输入爬取网址
ie.visible = 1;
source_info = ie.document.body.getElementsByClassName('classname') %classname 在浏览器中按F12,左上角选择元素点上,然后点选所需信息位置
爬虫流程方法2
使用webread函数直接读取该网页所有源代码,你所需要的信息就包含在里面,只不过因为是代码所以看起来杂乱无章,之后通过正则表达式匹配定位所需信息,提取这些信息,按顺序写入汇总表格。
简单以下图举个例子,我想获取价格信息,那么在源代码里面可以看到在“¥”符号之后跟着的数字就是价格。
正则表达式就可以写成 exp_price=’(?<=\¥)\d*.\d’。
它的意思就是从¥往后查找(数字(多位).数字(一位))这种规则的字符串,然后提取出来就是“6800.0”.

完整代码在这里,要注意网络响应时间这个参数设置方法。设置完之后再webread函数中记得也要写入实参options才有效。。
clear all;
%设置网络参数
options = weboptions;
options.Timeout=20; %默认是5s,容易出现连接响应超时而error.
%读取多页信息并汇总
txt_web=[];
for i=1:13
txt_web=[txt_web,webread(['网址page=',num2str(i)],options)]; %i对应页数,具体网址就不写了;不要忘记写options
end
%正则表达式
exp_character='(?<=职业\s).{2,3}';
exp_gender='(?<=性别\s).';
exp_level='(?<=\\r等级\s)\d{1,3}';
exp_camp='(?<=阵营\s).';
exp_primallevel='(?<=元神等级\s)\d{1,2}';
exp_attack='(?<=攻击\s)\d{1,6}\-\d{1,6}';
exp_defence='(?<=防御\s)\d{1,6}';
exp_blood='(?<=气血\s)\d{1,7}';
exp_magic='(?<=真气\s)\d{1,7}';
exp_name='(?<=\<dd\sstyle\=\"width\:145px\;\"\stitle\=\").{0,3}\*{0,3}';
exp_price='(?<=\¥)\d*\.\d';
exp_location='(?<=\<dd\sstyle\=\"width\:80px\;height\:65px\;\soverflow\:hidden\;\"\stitle\=\").{2,6}(?=\")';
exp_commid='(?<=class\=\"buy\_button\"\svalue\=\"购买\"\sonclick\=\"location\.href\=.)http\:\/\/www\.xunbao178\.com\:80\/zx\/getServerList\?commid\=\d{5,10}';
%匹配对应信息
character=regexp(txt_web,exp_character,'match');
character=reshape(character,[],1);
gender=regexp(txt_web,exp_gender,'match');
gender=reshape(gender,[],1);
level=regexp(txt_web,exp_level,'match');
level=reshape(level,[],1);
camp=regexp(txt_web,exp_camp,'match');
camp=reshape(camp,[],1);
primal_level=regexp(txt_web,exp_primallevel,'match');
primal_level=reshape(primal_level,[],1);
attack=regexp(txt_web,exp_attack,'match');
attack=reshape(attack,[],1);
defence=regexp(txt_web,exp_defence,'match');
defence=reshape(defence,[],1);
blood=regexp(txt_web,exp_blood,'match');
blood=reshape(blood,[],1);
magic=regexp(txt_web,exp_magic,'match');
magic=reshape(magic,[],1);
name=regexp(txt_web,exp_name,'match');
name=reshape(name,[],1);
price=regexp(txt_web,exp_price,'match');
price=reshape(price,[],1);
location=regexp(txt_web,exp_location,'match');
location=reshape(location,[],1);
commid=regexp(txt_web,exp_commid,'match');
commid=reshape(commid,[],1);
%信息写入表格
xlswrite('guiyun.xlsx',character,'A2:A180');
xlswrite('guiyun.xlsx',gender,'B2:B180');
xlswrite('guiyun.xlsx',level,'C2:C180');
xlswrite('guiyun.xlsx',camp,'D2:D180');
xlswrite('guiyun.xlsx',primal_level,'E2:E180');
xlswrite('guiyun.xlsx',attack,'F2:F180');
xlswrite('guiyun.xlsx',defence,'G2:G180');
xlswrite('guiyun.xlsx',blood,'H2:H180');
xlswrite('guiyun.xlsx',magic,'I2:I180');
xlswrite('guiyun.xlsx',name,'J2:J180');
xlswrite('guiyun.xlsx',price,'K2:K180');
xlswrite('guiyun.xlsx',location,'L2:L180');
xlswrite('guiyun.xlsx',commid,'M2:M180');
成果展示

所用函数
1.webread()
2.regexp()
3.reshape()
4.xlswrite()
很简单吧,我当时就是觉得就这么几个函数一定很简单,然后研究了老半天各种小问题- -…读正则表达式帮助就读了很久…
注意事项
1.记得写网络参数设置
2.有的网址输入进去,读取出来的信息仍然是首页,没有找到太好的解决办法- -。。
3.写正则表达式直接照着matlab帮助一点一点写就行。
4.正则表达式中慎用.*去匹配多个任意字符,会让运行速度大大降低.
5.记得调试正则表达式(遇到等级为空的一条信息,我匹配的是数字信息因此漏掉了。。)
下一步改进
我已经提取到了每个商品具体详情页的网址,那么可以用循环再爬取具体详情页里面的更多信息。
之后可以通过计算价格/重要属性,按自己想要的方式加权平均,获取一个综合性价比排序。但是目前这个网站很多属性并没有做好和游戏的接口,就有一些重要的属性看不到…一定是因为开新系统太快!!
勉强算是能在买号卖号的时候作为一个定价议价的参考吧…哈哈哈虽然不怎么玩了。。
实用性不怎么强,用来作为学习的例子倒是很有趣~
更新爬取详情页信息
按照昨天的想法,挨个获取详情页信息,并计算综合相对性价比.
遇到了快速多次访问,网站会跳回首页的问题,所以用pause()让访问慢一点。
大概测出来是1.1s左右一次就没问题,当然这个肯定是一种很小白的做法- -…
性价比计算直接建立了一个excel template,然后写入数据就可以了。

完整代码(matlab代码高亮选哪种语言会比较好看啊???):
tic
%设置网络参数
options = weboptions;
options.Timeout=20;
%读取多页信息并汇总
txt_web=[];
for i=1:10
txt_web=[txt_web,webread(['http://www.xunbao178.com/zx/search?keyWord=%E5%BD%92%E4%BA%91%20&orderBy=&&page=',num2str(i)],options)];
end
%正则表达式
exp_character='(?<=职业\s).{2,3}';
exp_gender='(?<=性别\s).';
exp_level='(?<=\\r等级\s)\d{1,3}';
exp_camp='(?<=阵营\s).';
exp_primallevel='(?<=元神等级\s)\d{1,2}';
exp_attack='(?<=攻击\s)\d{1,6}\-\d{1,6}';
exp_defence='(?<=防御\s)\d{1,6}';
exp_blood='(?<=气血\s)\d{1,7}';
exp_magic='(?<=真气\s)\d{1,7}';
exp_name='(?<=\<dd\sstyle\=\"width\:145px\;\"\stitle\=\").{0,3}\*{0,3}';
exp_price='(?<=\¥)\d*\.\d';
exp_location='(?<=\<dd\sstyle\=\"width\:80px\;height\:65px\;\soverflow\:hidden\;\"\stitle\=\").{2,6}(?=\")';
exp_commid='(?<=class\=\"buy\_button\"\svalue\=\"购买\"\sonclick\=\"location\.href\=.)http\:\/\/www\.xunbao178\.com\:80\/zx\/getServerList\?commid\=\d{5,10}';
%匹配对应信息
character=regexp(txt_web,exp_character,'match');
character=reshape(character,[],1);
gender=regexp(txt_web,exp_gender,'match');
gender=reshape(gender,[],1);
level=regexp(txt_web,exp_level,'match');
level=reshape(level,[],1);
camp=regexp(txt_web,exp_camp,'match');
camp=reshape(camp,[],1);
primal_level=regexp(txt_web,exp_primallevel,'match');
primal_level=reshape(primal_level,[],1);
attack=regexp(txt_web,exp_attack,'match');
attack=reshape(attack,[],1);
defence=regexp(txt_web,exp_defence,'match');
defence=reshape(defence,[],1);
blood=regexp(txt_web,exp_blood,'match');
blood=reshape(blood,[],1);
magic=regexp(txt_web,exp_magic,'match');
magic=reshape(magic,[],1);
name=regexp(txt_web,exp_name,'match');
name=reshape(name,[],1);
price=regexp(txt_web,exp_price,'match');
price=reshape(price,[],1);
location=regexp(txt_web,exp_location,'match');
location=reshape(location,[],1);
commid=regexp(txt_web,exp_commid,'match');
commid=reshape(commid,[],1);
%读取商品详情页信息
list_length=length(commid);
zb_name=cell(list_length,1);%装备主人
fb_name=cell(list_length,1);%法宝主人
cw_name=cell(list_length,1);%宠物主人
critical_damage=cell(list_length,1); %致命伤害
critical_rate=cell(list_length,1); %致命一击率
skill_evade=cell(list_length,1); %技能闪躲
skill_definition=cell(list_length,1); %技能命中
de_critical=cell(list_length,1); %减免他方致命率
de_critical_damage=cell(list_length,1); %减免致命伤害
de_damage_taoism=cell(list_length,1); %御仙
de_damage_evil=cell(list_length,1); %御魔
de_damage_buddhism=cell(list_length,1); %御佛
de_taoism=cell(list_length,1); %克仙
de_buddhism=cell(list_length,1); %克佛
de_evil=cell(list_length,1); %克魔
%详细信息正则表达式
exp_zb_name='(?<=装备主人\s).{1,10}(?=\\)'; %匹配两个字符中间的字符串
exp_fb_name='(?<=法宝主人\s).{1,10}(?=\\)';
exp_cw_name='(?<=宠物主人\"\svalue\=\").{1,10}(?=\")';
exp_critical_damage='(?<=\<critical\_damage\sname\=\"致命伤害\"\svalue\=\")\d{2,4}\.\d';
exp_critical_rate='(?<=\<critical\_rate\sname\=\"致命一击率\"\svalue\=\")\d{1,4}\.\d';
exp_skill_evade='(?<=\<skill\_evade\sname\=\"技能躲闪\"\svalue\=\")\d{1,4}\.\d';
exp_skill_definition='(?<=\<skill\_definition\sname\=\"技能命中\"\svalue\=\")\d{1,4}\.\d';
exp_de_critical='(?<=\<de\_critical\sname\=\"减免他方致命率\"\svalue\=\")\d{1,4}\.\d';
exp_de_critical_damage='(?<=\<de\_critical\_damage\sname\=\"减免致命伤害\"\svalue\=\")\d{1,4}\.\d';
exp_de_damage_taoism='(?<=\<de\_damage\_taoism\sname\=\"御仙\"\svalue\=\")\d{1,4}\.\d';
exp_de_damage_evil='(?<=\<de\_damage\_evil\sname\=\"御魔\"\svalue\=\")\d{1,4}\.\d';
exp_de_damage_buddhism='(?<=\<de\_damage\_buddhism\sname\=\"御佛\"\svalue\=\")\d{1,4}\.\d';
exp_de_taoism='(?<=\<de\_taoism\sname\=\"克仙\"\svalue\=\")\d{1,4}\.\d';
exp_de_buddhism='(?<=\<de\_buddhism\sname\=\"克佛\"\svalue\=\")\d{1,4}\.\d';
exp_de_evil='(?<=\<de\_evil\sname\=\"克魔\"\svalue\=\")\d{1,4}\.\d';
for i=1:list_length
pause(1.1); %降低访问速度
detail_info=webread(char(commid(i,1)),options);
try
zb_name(i,1)=unique(regexp(detail_info,exp_zb_name,'match'));
catch
zb_name(i,1)={'!没有装备主人!'};
end
try
fb_name(i,1)=unique(regexp(detail_info,exp_fb_name,'match'));
catch
fb_name(i,1)={'!没有法宝主人!'};
end
try
cw_name(i,1)=unique(regexp(detail_info,exp_cw_name,'match'));
catch
cw_name(i,1)={'!没有宠物主人!'};
end
try
critical_damage(i,1)=regexp(detail_info,exp_critical_damage,'match');
catch
critical_damage(i,1)={0};
end
try
critical_rate(i,1)=regexp(detail_info,exp_critical_rate,'match');
catch
critical_rate(i,1)={0};
end
try
skill_evade(i,1)=regexp(detail_info,exp_skill_evade,'match');
catch
skill_evade(i,1)={0};
end
try
skill_definition(i,1)=regexp(detail_info,exp_skill_definition,'match');
catch
skill_definition(i,1)={0};
end
try
de_critical(i,1)=regexp(detail_info,exp_de_critical,'match');
catch
de_critical(i,1)={0};
end
try
de_critical_damage(i,1)=regexp(detail_info,exp_de_critical_damage,'match');
catch
de_critical_damage(i,1)={0};
end
try
de_damage_taoism(i,1)=regexp(detail_info,exp_de_damage_taoism,'match');
catch
de_damage_taoism(i,1)={0};
end
try
de_damage_evil(i,1)=regexp(detail_info,exp_de_damage_evil,'match');
catch
de_damage_evil(i,1)={0};
end
try
de_damage_buddhism(i,1)=regexp(detail_info,exp_de_damage_buddhism,'match');
catch
de_damage_buddhism(i,1)={0};
end
try
de_taoism(i,1)=regexp(detail_info,exp_de_taoism,'match');
catch
de_taoism(i,1)={0};
end
try
de_buddhism(i,1)=regexp(detail_info,exp_de_buddhism,'match');
catch
de_buddhism(i,1)={0};
end
% try
de_evil(i,1)=regexp(detail_info,exp_de_evil,'match'); %match失败既代表访问失败
% catch
% de_evil(i,1)={0};
% end
end
toc
% 信息写入表格
xlswrite('zhuxian_template.xltx',character,'A2:A180');
xlswrite('zhuxian_template.xltx',gender,'B2:B180');
xlswrite('zhuxian_template.xltx',level,'C2:C180');
xlswrite('zhuxian_template.xltx',camp,'D2:D180');
xlswrite('zhuxian_template.xltx',primal_level,'E2:E180');
xlswrite('zhuxian_template.xltx',attack,'F2:F180');
xlswrite('zhuxian_template.xltx',defence,'G2:G180');
xlswrite('zhuxian_template.xltx',blood,'H2:H180');
xlswrite('zhuxian_template.xltx',magic,'I2:I180');
xlswrite('zhuxian_template.xltx',name,'J2:J180');
xlswrite('zhuxian_template.xltx',price,'K2:K180');
xlswrite('zhuxian_template.xltx',location,'L2:L180');
xlswrite('zhuxian_template.xltx',commid,'M2:M180');
xlswrite('zhuxian_template.xltx',zb_name,'N2:N180');
xlswrite('zhuxian_template.xltx',critical_damage,'O2:O180');
xlswrite('zhuxian_template.xltx',critical_rate,'P2:P180');
xlswrite('zhuxian_template.xltx',skill_evade,'Q2:Q180');
xlswrite('zhuxian_template.xltx',skill_definition,'R2:R180');
xlswrite('zhuxian_template.xltx',de_critical,'S2:S180');
xlswrite('zhuxian_template.xltx',de_critical_damage,'T2:T180');
xlswrite('zhuxian_template.xltx',de_damage_taoism,'U2:U180');
xlswrite('zhuxian_template.xltx',de_damage_evil,'V2:V180');
xlswrite('zhuxian_template.xltx',de_damage_buddhism,'W2:W180');
xlswrite('zhuxian_template.xltx',de_taoism,'X2:X180');
xlswrite('zhuxian_template.xltx',de_buddhism,'Y2:Y180');
xlswrite('zhuxian_template.xltx',de_evil,'Z2:Z180');
xlswrite('zhuxian_template.xltx',fb_name,'AA2:AA180');
xlswrite('zhuxian_template.xltx',cw_name,'AB2:AB180');
本文介绍了如何使用Matlab进行简单的网络爬虫,通过两种方法抓取网页上的角色信息,包括利用正则表达式提取价格等关键数据。在实践中,作者提醒注意网络响应时间设置和正则表达式的优化,以及遇到的挑战和解决方案。

994

被折叠的 条评论
为什么被折叠?



