//引入模块
var http = require("http"),
fs = require(‘fs‘),
url = require(‘url‘);
//写入文件,把结果写入不同的文件
var writeRes = function(p, r) {
fs.appendFile(p , r, function(err) {
if(err)
console.log(err);
else
console.log(r);
});
},
//发请求,并验证内容,把结果写入文件
postHttp = function(arr, num) {
console.log(‘第‘+num+"条!")
var a = arr[num].split(" - ");
if(!a[0] || !a[1]) {
return;
}
var address = url.parse(a[1]),
options = {
host : address.host,
path: address.path,
hostname : address.hostname,
method: ‘GET‘,
headers: {
‘User-Agent‘ : ‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36‘
}
}
var req = http.request(options, function(res) {
if (res.statusCode == 200) {
res.setEncoding(‘UTF-8‘);
var data = ‘‘;
res.on(‘data‘, function (rd) {
data += rd;
});
res.on(‘end‘, function(q) {
if(!~data.indexOf("www.baidu.com")) {
return writeRes(‘./no2.txt‘, a[0] + ‘--‘ + a[1] + ‘\n‘);
} else {
return writeRes(‘./has2.txt‘, a[0] + ‘--‘ + a[1] + "\n");
}
})
} else {
writeRes(‘./error2.txt‘, a[0] + ‘--‘ + a[1] + ‘--‘ + res.statusCode + ‘\n‘);
}
});
req.on(‘error‘, function(e) {
writeRes(‘./error2.txt‘, a[0] + ‘--‘ + a[1] + ‘--‘ + e + ‘\n‘);
})
req.end();
},
//读取文件,获取需要抓取的页面
openFile = function(path, coding) {
fs.readFile(path, coding, function(err, data) {
var res = data.split("\n");
for (var i = 0, rl = res.length; i < rl; i++) {
if(!res[i])
continue;
postHttp(res, i);
};
})
};
openFile(‘./sites.log‘, ‘utf-8‘);
上面的代码大家应该都能看的懂!这里只是一个实验性的一些代码,具体的还要大家自己去研究研究!
原文:http://www.cnblogs.com/xjser/p/4968866.html