Previous on 系列教程:ajax
互联网金融爬虫怎么写-第一课 p2p网贷爬虫(XPath入门)正则表达式
互联网金融爬虫怎么写-第二课 雪球网股票爬虫(正则表达式入门)json
互联网金融爬虫怎么写-第三课 雪球网股票爬虫(ajax分析)浏览器
哈哈,我又来了,话说出教程就是这么任性,我们乘热打铁,把上节课分析完成可是没写的代码给完成了!cookie
工具要求与基础知识:框架
工具要求:dom
教程中主要使用到了 一、神箭手云爬虫 框架 这个是爬虫的基础,二、Chrome浏览器和Chrome的插件XpathHelper 这个用来测试Xpath写的是否正确 三、Advanced REST Client用来模拟提交请求工具
基础知识:学习
本教程中主要用到了一些基础的js和xpath语法,若是对这两种语言不熟悉,能够提早先学习下,都很简单。测试
三步走
还记得咱们在遥远的电商系列爬虫教程的第一课里提到具体写爬虫的几个步骤吗?咱们沿着路径再来走一遍:
第一步:肯定入口URL
暂且使用这个第一页的ajax的url连接:
http://xueqiu.com/stock/cata/stocklist.json?page=1&size=30&order=desc&orderby=percent&type=11%2C12
第二步:区份内容页和中间页
此次你们有点犯难了,虽说每个股票都有一个单独的页面,可是列表页的信息已经蛮多的了,光爬取列表页信息就已经够了,那怎么区份内容页和中间页呢?其实咱们只须要将内容页和中间页的正则设置成同样的既可。以下:
http://xueqiu.com/stock/cata/stocklist\\.json\\?page=\\d+&size=30&order=desc&orderby=percent&type=11%2C12
在提醒你们一下,这里之因此转义符用了两个是由于在神箭手中,设置正则时,是字符串设置,须要对转义符再作一次转义。
第三步:内容页抽取规则
因为ajax返回的是json,而神箭手是支持jsonpath的提取方式的,所以提取规则就很简单了。不过这里要特殊注意的是,因为咱们是在列表页抽取数据,所以数据最顶层至关因而一个列表,咱们须要在顶层的field上设置一个列表数据的值。具体抽取规则以下:
fields: [
{
name:"stocks",
selector:"$.stocks",
selectorType:SelectorType.JsonPath,
repeated:true,
children:[
{
name:"code",
alias:"代码",
selector:"$.code",
selectorType:SelectorType.JsonPath,
},
{
name:"name",
alias:"名称",
selector:"$.name",
selectorType:SelectorType.JsonPath,
},
{
name:"current",
alias:"当前价格",
selector:"$.current",
selectorType:SelectorType.JsonPath,
},
{
name:"high",
alias:"最高价格",
selector:"$.high",
selectorType:SelectorType.JsonPath,
},
{
name:"low",
alias:"最低价格",
selector:"$.low",
selectorType:SelectorType.JsonPath,
}
]
}
]
我简单抽取了一些信息,其余信息都相似。
好了,主要的代码基本已经写好了,剩下的还须要解决两个问题
1.爬取前须要先访问一下首页获取cookie
2.虽然能够直接加入下一页,可是一共有多少页并不知道。
首先对于第一点,咱们只须要在beforeCrawl回调中访问一下首页便可,神箭手会自动对cookie进行处理和保存,具体代码以下:
configs.beforeCrawl =function(site){
site.requestUrl("http://xueqiu.com");
};
好了,除了下一页基本已经没什么问题了,咱们先测试一下看看效果:
数据已经出来了,没问题,第一页的数据都有了,那下一页怎么处理呢?咱们有两个方案:
第一个方案:
咱们能够看到json的返回值中有一个count字段,这个字段目测应该是总数据量的值,那没咱们根据这个值,再加上单页数据条数,咱们就能够判断总共有多少页了。
第二个方案:
咱们先访问一下,假设页数很大,看看会雪球会返回什么,咱们尝试访问第500页,能够看到返回值中的stocks是0个,那么咱们能够根据是否有数据来判断需不须要加下一页。
两个方案各有利弊,咱们这里选择用第一个方案来处理,具体代码以下:
configs.onProcessHelperPage =function(page, content, site){
if(page.url.indexOf("page=1&size=30") !== -1){
//若是是第一页
varresult = JSON.parse(page.raw);
varcount = result.count.count;
varpage_num = Math.ceil(count/30);
if(page_num > 1){
for(vari = 2;i<=page_num;i++){
site.addUrl("http://xueqiu.com/stock/cata/stocklist.json?page="+i+"&size=30&order=desc&orderby=percent&type=11%2C12");
}
}
}
};
好了,经过三课的艰苦奋战,终于完成了雪球沪深一览的征服。先看下跑出来的效果。
完整代码以下:
varconfigs = {
domains: ["xueqiu.com"],
scanUrls: ["http://xueqiu.com/stock/cata/stocklist.json?page=1&size=30&order=desc&orderby=percent&type=11%2C12"],
contentUrlRegexes: ["http://xueqiu.com/stock/cata/stocklist\\.json\\?page=\\d+&size=30&order=desc&orderby=percent&type=11%2C12"],
helperUrlRegexes: ["http://xueqiu.com/stock/cata/stocklist\\.json\\?page=\\d+&size=30&order=desc&orderby=percent&type=11%2C12"],
fields: [
{
name:"stocks",
selector:"$.stocks",
selectorType:SelectorType.JsonPath,
repeated:true,
children:[
{
name:"code",
alias:"代码",
selector:"$.code",
selectorType:SelectorType.JsonPath,
},
{
name:"name",
alias:"名称",
selector:"$.name",
selectorType:SelectorType.JsonPath,
},
{
name:"current",
alias:"当前价格",
selector:"$.current",
selectorType:SelectorType.JsonPath,
},
{
name:"high",
alias:"最高价格",
selector:"$.high",
selectorType:SelectorType.JsonPath,
},
{
name:"low",
alias:"最低价格",
selector:"$.low",
selectorType:SelectorType.JsonPath,
}
]
}
]
};
configs.onProcessHelperPage =function(page, content, site){
if(page.url.indexOf("page=1&size=30") !== -1){
//若是是第一页
varresult = JSON.parse(page.raw);
varcount = result.count.count;
varpage_num = Math.ceil(count/30);
if(page_num > 1){
for(vari = 2;i<=page_num;i++){
site.addUrl("http://xueqiu.com/stock/cata/stocklist.json?page="+i+"&size=30&order=desc&orderby=percent&type=11%2C12");
}
}
}
};
configs.beforeCrawl =function(site){
site.requestUrl("http://xueqiu.com");
};
varcrawler =newCrawler(configs);
crawler.start();
这样咱们的雪球网股票爬虫就算大功告成,固然咱们还能够把type的设置模板化。不过这个是一些高级的方法,咱们会在后面的课程中再去详细描述
最后,对爬虫感兴趣的童鞋欢迎加qq群跟我讨论:566855261。