Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
497 views
in Technique[技术] by (71.8m points)

用Nodejs抓虫遇到一个循环请求问题

用Nodejs抓虫遇到一个循环请求问题,目前只获取到第一个分页,这个问题怎么获取分页总数,然后获取每一页的页面数据?

var express = require('express');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var app = express();

app.get('/',function (req,res) {
    var id = 1;
    url = 'http://www.zcool.com.cn/zteam/ZNDg2MzI=.html?sort=0&p='+ id +'#titleMaoDian';
    console.log(id);
    request(url,function(err,res,html){
        if(!err){
            var $ = cheerio.load(html);
            var endId = $('.bigPage').find('a.pageNext').prev().text();
            for(id=1;id<=endId;id++){
                var title;
                $('.camLiTitleC').filter(function(){
                    var data = $(this);
                    title = data.text();
                    console.log('作品标题:' + title);
                })
            }
        }
    })
});

app.listen('8080'); //使用8080端口
exports = module.exports = app;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

先将url后缀空出数字部分,然后可以定义一个函数,再递归调用自身,每次加一
具体操作可以看下我的文章(羞涩..)Node.js实现简易爬虫


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...