var request = require('request'),
cheerio = require('cheerio');
request('https://cnodejs.org/', function(err, response, body){
if( !err && response.statusCode == 200 ){
// body为源码
// 使用 cheerio.load 将字符串转换为 cheerio(jQuery) 对象,
// 按照jQuery方式操作即可
var $ = cheerio.load(body);
// 输出导航的html代码
console.log( $('.nav').html() );
}
});
{
title : $item.find('.topic_title').text(),
url : $item.find('.topic_title').attr('href'),
author : $item.find('.user_avatar img').attr('title'),
reply : $item.find('.count_of_replies').text(),
visits : $item.find('.count_of_visits').text()
}
request('https://cnodejs.org/?_t='+Date.now(), function(err, response, body){
if( !err && response.statusCode == 200 ){
var $ = cheerio.load(body);
var data = [];
$('#topic_list .cell').each(function(){
var $this = $(this);
// 使用trim去掉数据两端的空格
data.push({
title : trim($this.find('.topic_title').text()),
url : trim($this.find('.topic_title').attr('href')),
author : trim($this.find('.user_avatar img').attr('title')),
reply : trim($this.find('.count_of_replies').text()),
visits : trim($this.find('.count_of_visits').text())
})
});
// console.log( JSON.stringify(data, ' ', 4) );
console.log(data);
}
});
// 删除字符串左右两端的空格
function trim(str){
return str.replace(/(^s*)|(s*$)/g, "");
}
// 把page作为参数传递进去,然后调用request进行抓取
function getData(page){
var url = 'https://cnodejs.org/?tab=all&page='+page;
console.time(url);
request(url, function(err, response, body){
if( !err && response.statusCode == 200 ){
console.timeEnd(url); // 通过time和timeEnd记录抓取url的时间
var $ = cheerio.load(body);
var data = [];
$('#topic_list .cell').each(function(){
var $this = $(this);
data.push({
title : trim($this.find('.topic_title').text()),
url : trim($this.find('.topic_title').attr('href')),
author : trim($this.find('.user_avatar img').attr('title')),
reply : trim($this.find('.count_of_replies').text()),
visits : trim($this.find('.count_of_visits').text())
})
});
// console.log( JSON.stringify(data, ' ', 4) );
// console.log(data);
var filename = './file/cnode_'+page+'.txt';
fs.writeFile(filename, JSON.stringify(data, ' ', 4), function(){
console.log( filename + ' 写入成功' );
})
}
});
}
var max = 6;
for(var i=1; i<=max; i++){
getData(i);
}
$ node test.js 开始请求... https://cnodejs.org/?tab=all&page=1: 279ms ./file/cnode_1.txt 写入成功 https://cnodejs.org/?tab=all&page=3: 372ms ./file/cnode_3.txt 写入成功 https://cnodejs.org/?tab=all&page=2: 489ms ./file/cnode_2.txt 写入成功 https://cnodejs.org/?tab=all&page=4: 601ms ./file/cnode_4.txt 写入成功 https://cnodejs.org/?tab=all&page=5: 715ms ./file/cnode_5.txt 写入成功 https://cnodejs.org/?tab=all&page=6: 819ms ./file/cnode_6.txt 写入成功
/*
@param data [] 需要请求的链接的集合
@param max num 最多同时请求的数量
*/
function Dispatch(data, max){
var _max = max || 5, // 最多请求的数量
_dataObj = data || [], // 需要请求的url集合
_cur = 0, // 当前请求的个数
_num = _dataObj.length || 0,
_isEnd = false,
_callback;
var ss = function(){
var s = _max - _cur;
while(s--){
if( !_dataObj.length ){
_isEnd = true;
break;
}
var surl = _dataObj.shift();
_cur++;
_callback(surl);
}
}
this.start = function(callback){
_callback = callback;
ss();
},
this.call = function(){
if( !_isEnd ){
_cur--;
ss();
}
}
}
var dis = new Dispatch(urls, max);
dis.start(getData);
var filename = './file/cnode_'+page+'.txt';
fs.writeFile(filename, JSON.stringify(data, ' ', 4), function(){
console.log( filename + ' 写入成功' );
})
dis.call();
request({
url:'https://www.zhihu.com/explore',
headers:{
// "Referer":"www.zhihu.com"
cookie : xxx
}
}, function(error, response, body){
if (!error && response.statusCode == 200) {
// console.log( body );
var $ = cheerio.load(body);
}
})
var request = require('request'),
cheerio = require('cheerio'),
fs = require('fs'),
path = require('path'), // 用于分析图片的名称或者后缀名
mkdirp = require('mkdirp'); // 用于创建多级目录
var date = new Date(),
year = date.getFullYear(),
month = date.getMonth()+1,
month = ('00'+month).slice(-2), // 添加前置0
day = date.getDate(),
day = ('00'+day).slice(-2), // 添加前置0
dir = './img/'+year+'/'+month+'/'+day+'/';
// 根据日期创建目录 ./img/2017/01/22/
var stats = fs.statSync(dir);
if( stats.isDirectory() ){
console.log(dir+' 已存在');
}else{
console.log('正在创建目录 '+dir);
mkdirp(dir, function(err){
if(err) throw err;
})
}
request({
url : 'http://desk.zol.com.cn/meinv/?_t='+Date.now()
}, function(err, response, body){
if(err) throw err;
if( response.statusCode == 200 ){
var $ = cheerio.load(body);
$('.photo-list-padding img').each(function(){
var $this = $(this),
imgurl = $this.attr('src');
var ext = path.extname(imgurl); // 获取图片的后缀名,如 .jpg, .png .gif等
var filename = Date.now()+'_'+ parseInt(Math.random()*10000)+ext; // 命名方式:毫秒时间戳+随机数+后缀名
// var filename = path.basename(imgurl); // 直接获取图片的原名称
// console.log(filename);
download(imgurl, dir+filename); // 开始下载图片
})
}
});
// 保存图片
var download = function(imgurl, filename){
request.head(imgurl, function(err, res, body) {
request(imgurl).pipe(fs.createWriteStream(filename));
console.log(filename+' success!');
});
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有