您的位置:首页 > Web前端 > Node.js

node爬虫

2014-11-14 19:13 211 查看
最近想爬页面(http://detail.tmall.com/item.htm?id=16190874665&spm=a1z09.2.9.200.dAzBgD&_u=d3gf2n0b5ee&mt=)中的促销价。不想用之前的技术,尝试用js做一下。

1、用一个页面请求目标页,返回源码,使用jquery的跨域,只是提示语法错误<,不能识别第一个<。

2、用node做后端试试。因为是爬淘宝的数据,面对的是阿里,它把能加的技术都加上了。

一、用正常的get请求,返回302,找到request:https://github.com/request/request,拿到页面。

二、从返回的页面中得到url1,再次发get请求,指向错误。对比了一下request header,发现请求头中少了几个字段。要修改请求头,找到了nodegrass:https://github.com/scottkiss/nodegrass。

三、根据nodegrass中添加header的方法,想到request中也可以使用这种方法来添加header,用以下代码实现。再看了一下node的文档,有原生的request(),再次加深了一下理解。看教程讲的不是很全面和详细,看文档讲的不具体,所以还是要结合看,都可信,都不可全信。

var header = {
'Cache-Control': 'max-age=0',
'Referer': 'http://detail.tmall.com/item.htm?id=16190874665&spm=a1z09.2.9.200.dAzBgD&_u=d3gf2n0b5ee&mt='
};

var option = {
headers: header
}

request(res, option, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}
})


20141129爬虫

http.get(url, function(res){
res.on('data', function(chunk){
chunks.push(chunk);
size +=chunk.length;
});
res.on('end', function(){
var data = Buffer.concat(chunks, size);
var html = new Buffer(data);
var $ = cheerio.load(html);
for(var i=0;i<$(selector).length;i++){   //$(selector).length
(function(num) {
var cata_item = {};
cata_item.id = num;
cata_item.url = 'http://www.e-cantonfair.com'+$(selector).eq(num).attr('href').replace('.html','/suppliers.html');
cata_item.child = [];
cata_item.name = $(selector).eq(num).text();
cata_item.pages = 0;
cata_item.pages_t = 0;
cata.push(cata_item);
// console.log('cata_url:'+cata[0].url);
})(i);
}
for(var i=0; i<cata.length; i++){
pachong_1(cata[i], cata[i].url.replace('/suppliers.html','-1/suppliers.html'), '.btn.btn-inquire.fr');
}
})
}).on('error', function(e){
console.log('error:'+e.message);
});


原生的get请求,使用cheerio处理页面。技术上没什么说的。有几点需要记录一下:

1、使用excel-export输出到excel中,官网上使用express的get请求来输出,可以等所有的都返回完了之后在输出(不用get请求)

conf.stylesXmlFile = "styles.xml";
conf.cols = [
{
caption:'company_name',
type:'string',
beforeCellWrite:function(row, cellData){
return cellData;
},
width:30
},
for(var j=0; j<cata.length; j++){
conf.rows = [];
for(var i=0;i<cata[j].child.length;i++){
conf.rows[i] = [];
conf.rows[i][0]=cata[j].child[i].name;
}
var result = nodeExcel.execute(conf);
try{
fs.writeFileSync('excel/'+cata[j].name+'.xlsx', result, 'binary');
}catch(e){
console.log('error:'+e.message);
}
}


  

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac"><fonts count="2" x14ac:knownFonts="1"><font><sz val="11"/><color theme="1"/><name val="Calibri"/><family val="2"/><scheme val="minor"/></font><font><sz val="11"/><color theme="1"/><name val="Calibri"/><family val="2"/><scheme val="minor"/></font></fonts><fills count="3"><fill><patternFill patternType="none"/></fill><fill><patternFill patternType="gray125"/></fill><fill><patternFill patternType="solid"><fgColor rgb="FFFFFFCC"/></patternFill></fill></fills><borders count="2"><border><left/><right/><top/><bottom/><diagonal/></border><border><left style="thin"><color rgb="FFB2B2B2"/></left><right style="thin"><color rgb="FFB2B2B2"/></right><top style="thin"><color rgb="FFB2B2B2"/></top><bottom style="thin"><color rgb="FFB2B2B2"/></bottom><diagonal/></border></borders><cellStyleXfs count="2"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/><xf numFmtId="0" fontId="1" fillId="2" borderId="1" applyNumberFormat="0" applyFont="0" applyAlignment="0" applyProtection="0"/></cellStyleXfs><cellXfs count="4"><xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/><xf numFmtId="14" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1"/><xf numFmtId="14" fontId="0" fillId="2" borderId="1" xfId="1" applyNumberFormat="1" applyFont="1"/><xf numFmtId="0" fontId="0" fillId="2" borderId="1" xfId="1" applyFont="1"/></cellXfs><cellStyles count="2"><cellStyle name="Normal" xfId="0" builtinId="0"/><cellStyle name="Note" xfId="1" builtinId="10"/></cellStyles><dxfs count="0"/><tableStyles count="0" defaultTableStyle="TableStyleMedium2" defaultPivotStyle="PivotStyleLight16"/><extLst><ext uri="{EB79DEF2-80B8-43e5-95BD-54CBDDF9020C}" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main"><x14:slicerStyles defaultSlicerStyle="SlicerStyleLight1"/></ext></extLst></styleSheet>


2、大量的爬虫,每一步都需要详细记录,以备出现问题后,可以准确定位。要步步为营,步步测试。在这次爬虫中就犯了一个错误,导致n次请求发送了1*2*。。。*n次。

3、希望抓取的数据能直接存入mongodb,再转为其他格式。

4、这次的爬虫都是通过url抓取的,其中有事件触发加载的数据,但是通过chrome的network找到了url,上一次的爬虫是与服务器直接交互的。

5、感觉自己写的代码不是很简洁,多看看别人的爬虫是如何写的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: