Matlab简单爬虫-寻宝天行诛仙在售角色信息

  • 主要内容
    • 爬虫流程方法1
    • 爬虫流程方法2
    • 所用函数
    • 注意事项
    • 下一步改进
    • 更新爬取详情页信息

主要内容

偶然在网上看到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:10txt_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_lengthpause(1.1);  %降低访问速度detail_info=webread(char(commid(i,1)),options);tryzb_name(i,1)=unique(regexp(detail_info,exp_zb_name,'match'));catchzb_name(i,1)={'!没有装备主人!'};endtryfb_name(i,1)=unique(regexp(detail_info,exp_fb_name,'match'));catchfb_name(i,1)={'!没有法宝主人!'};endtrycw_name(i,1)=unique(regexp(detail_info,exp_cw_name,'match'));catchcw_name(i,1)={'!没有宠物主人!'};endtrycritical_damage(i,1)=regexp(detail_info,exp_critical_damage,'match');catchcritical_damage(i,1)={0};endtrycritical_rate(i,1)=regexp(detail_info,exp_critical_rate,'match');catchcritical_rate(i,1)={0};endtryskill_evade(i,1)=regexp(detail_info,exp_skill_evade,'match');catchskill_evade(i,1)={0};endtryskill_definition(i,1)=regexp(detail_info,exp_skill_definition,'match');catchskill_definition(i,1)={0};endtryde_critical(i,1)=regexp(detail_info,exp_de_critical,'match');catchde_critical(i,1)={0};endtryde_critical_damage(i,1)=regexp(detail_info,exp_de_critical_damage,'match');catchde_critical_damage(i,1)={0};endtryde_damage_taoism(i,1)=regexp(detail_info,exp_de_damage_taoism,'match');catchde_damage_taoism(i,1)={0};endtryde_damage_evil(i,1)=regexp(detail_info,exp_de_damage_evil,'match');catchde_damage_evil(i,1)={0};endtryde_damage_buddhism(i,1)=regexp(detail_info,exp_de_damage_buddhism,'match');catchde_damage_buddhism(i,1)={0};endtryde_taoism(i,1)=regexp(detail_info,exp_de_taoism,'match');catchde_taoism(i,1)={0};endtryde_buddhism(i,1)=regexp(detail_info,exp_de_buddhism,'match');catchde_buddhism(i,1)={0};end
%     tryde_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简单爬虫-寻宝天行诛仙在售角色信息相关推荐

  1. 基于人人网的简单爬虫(一)——正则表达式

    应课程实验要求,要写一个基于人人网的简单爬虫.实验要求如下: 学会使用一种编程语言实现爬取人人网关系网络的程序.该程序功能如下: 1.  能够输入用户登陆所产生的cookie,允许爬虫对人人网进行爬取 ...

  2. python_2开发简单爬虫

    2017年12月03日 16:43:01 独行侠的守望 阅读数:204 标签: python爬虫 更多 个人分类: Python 编辑 版权声明:本文为博主原创文章,转载请注明文章链接. https: ...

  3. Golang实现简单爬虫框架(4)——队列实现并发任务调度

    前言 在上一篇文章<Golang实现简单爬虫框架(3)--简单并发版>中我们实现了一个最简单并发爬虫,调度器为每一个Request创建一个goroutine,每个goroutine往Wor ...

  4. 图像隐藏matlab代码,picture Matlab简单操作实现图像的隐藏加密 联合开发网 - pudn.com...

    picture 所属分类:其他 开发工具:Others 文件大小:15KB 下载次数:1 上传日期:2018-07-18 09:50:48 上 传 者:至尊宝物语 说明:  Matlab简单操作实现图 ...

  5. python简单爬虫程序分析_[Python专题学习]-python开发简单爬虫

    掌握开发轻量级爬虫,这里的案例是不需要登录的静态网页抓取.涉及爬虫简介.简单爬虫架构.URL管理器.网页下载器(urllib2).网页解析器(BeautifulSoup) 一.爬虫简介以及爬虫的技术价 ...

  6. 使用HttpClient实现一个简单爬虫,抓取煎蛋妹子图

    第一篇文章,就从一个简单爬虫开始吧. 这只虫子的功能很简单,抓取到"煎蛋网xxoo"网页(http://jandan.net/ooxx/page-1537),解析出其中的妹子图,保 ...

  7. 第3章 简单爬虫架构

    第一节 python简单爬虫架构 1.爬虫调度端 2. 爬虫:URL管理器.网页下载器.网页解析器 3.价值数据 第二节 python简单爬虫架构的动态 转载于:https://www.cnblogs ...

  8. 【Python】学习笔记总结7(简单爬虫)

    文章目录 七.Python简单爬虫 1.重要知识与技能 2.使用re表达式抓取网页文件 3.使用requests抓取网页 4.使用re正则表达式提取数据 5.使用xPath工具提取数据 6.使用Bea ...

  9. .net core 实现简单爬虫—抓取博客园的博文列表

    一.介绍一个Http请求框架HttpCode.Core HttpCode.Core 源自于HttpCode(传送门),不同的是 HttpCode.Core是基于.net standard 2.0实现的 ...

  10. python 百度百科 爬虫_python简单爬虫

    爬虫真是一件有意思的事儿啊,之前写过爬虫,用的是urllib2.BeautifulSoup实现简单爬虫,scrapy也有实现过.最近想更好的学习爬虫,那么就尽可能的做记录吧.这篇博客就我今天的一个学习 ...

最新文章

  1. (干货)微信小程序转发好友
  2. 《高性能Linux服务器构建实战》笔记
  3. ORACLE中的包和包体
  4. 配置单节点伪分布式Hadoop
  5. 不容易系列之(4)——考新郎
  6. 算法笔记---从N个整数中选取K个数平方和最大(加可重复选值)
  7. 云计算 码率适配限速_5G 成熟后,带宽足够且云计算力够大,会不会对 PC 的升级需求放缓?...
  8. linux eclipse中文设置字体,完美调整Ubuntu下的Eclipse字体及界面显示
  9. Java后台生成NO2016012701(代码+年月日+流水号)这样的流水编号
  10. linux centos7 iso镜像下载,CentOS 7镜像文件下载
  11. ppt科研绘图之通过vba一键导出pdf
  12. Error connecting to node kafka:9092 (id: 1001 rack: null)
  13. linux下ScrollLock键盘灯不亮
  14. win10计算机用户怎么删除,Win10系统怎么管理的家庭成员账户? Win10删除账户的教程...
  15. 一款简洁的 image-crop.js图片裁剪工具
  16. Android开发实例家庭理财通,基于安卓的家庭理财通
  17. Android 实现书籍翻页效果----原理篇
  18. 『JavaSE』多态
  19. 阿里云OSS存储服务器
  20. 得到课程:高效学习-脱不花

热门文章

  1. 嵌入式linux:通过qemu模拟mini2440开发环境
  2. 高性能web 架构之 mysql 读写分离
  3. 在计算机中正确的邮件写信格式,电子邮件格式怎么写
  4. Swipe 移动端滑动插件使用详解
  5. McAfee设置信任文件
  6. 幸福课第11讲_笔记
  7. 论技术交流的重要性,两个PMOS管背靠背用法详解
  8. android系统级别应用开发要点
  9. android高德地图语音,高德地图导航组件没有导航语音声音
  10. write函数的详解与read函数的详解