使用async异步抓取数据,遇到了个问题,大家帮忙看下吧
发布于 6个月前 作者 qianjiahao 350 次浏览 来自 问答
    var express = require('express'),
    superagent = require('superagent'),
    cheerio = require('cheerio'),
    async = require('async');

    var app = new express(),
    targetUrl = 'https//cnodejs.org/';

    app.get('/',function(req,res,next){
    async.parallel({
        one:function(callback){
            superagent.get(targetUrl)
                .end(function(err,data){
                    if(err) return next(err);

                    var $ = cheerio.load(data.text);
                    var array1 = [];

                    $('#topic_list .topic_title').each(function(index,element){
                        var $element = $(element);
                        array1.push({
                            title:$element.attr('title')
                        });
                    });
                    callback(null,array1);
                });
        },
        two:function(callback){
            superagent.get(targetUrl)
                .end(function(err,data){
                    if(err) return next(err);

                    var $ = cheerio.load(data.text);
                    var array2 = [];

                    $('#topic_list .topic_title').each(function(index,element){
                        var $element = $(element);
                        array2.push({
                            href:$element.attr('href')
                        });
                    });
                    callback(null,array2);
                });
        }
    },function(err,result){
        if(err){
            console.log(err);
        }
        var obj = {
            array1:result.array1,
            array2:result.array2
        }
        res.send(obj);
    });
    });

    app.listen(3000,function(){
    console.log('server start...');
    });

错误为 TypeError: Cannot read property ‘request’ of undefined at Request.request (/Users/qianjiahao/crawler/node_modules/superagent/lib/node/index.js:716:27) at Request.end (/Users/qianjiahao/crawler/node_modules/superagent/lib/node/index.js:805:18) at Object.async.parallel.one (/Users/qianjiahao/crawler/app.js:34:6) at /Users/qianjiahao/crawler/node_modules/async/lib/async.js:583:25 at /Users/qianjiahao/crawler/node_modules/async/lib/async.js:125:13 at Array.forEach (native) at _each (/Users/qianjiahao/crawler/node_modules/async/lib/async.js:46:24) at Object.async.each (/Users/qianjiahao/crawler/node_modules/async/lib/async.js:124:9) at _parallel (/Users/qianjiahao/crawler/node_modules/async/lib/async.js:582:20) at Object.async.parallel (/Users/qianjiahao/crawler/node_modules/async/lib/async.js:598:9)

谁能指点一下,第一次用async,不太熟悉,谢谢。

6 回复

你装了 Request 么? npm install request

看起来应该是模块的依赖但是没装上… https://github.com/visionmedia/superagent/blob/master/package.json 里没有 request 手动装一下试试看

@phoenixlzx 好像没有用哎,看这个报错,不知道是哪里的错误,好郁闷啊

错误日志上面写了,你仔细看下

… 我又看了一遍代码

targetUrl = 'https//cnodejs.org/';

@phoenixlzx 我的天。。。谢谢

回到顶部