无法阅读财产'父母' of undefined - Node.js

时间:2015-01-02 22:44:19

标签: javascript node.js cheerio superagent

我试图利用superagent和cheerio来抓取来自kijiji的数据进行学习,而我得到的错误是"无法读取属性' parent'未定义"。 (我试过craigslist很好)。它发生在我得到正确的结果后#34;对象"。 cheerio似乎有些不对劲。有没有人对此有任何想法?提前感谢您的帮助。

[ERROR] 17:30:24 TypeError
TypeError: Cannot read property 'parent' of undefined
    at Function.exports.update (C:\Users\zhi_xie\nodejs\neomovie\node_modules\ch
eerio\lib\parse.js:55:25)
    at module.exports (C:\Users\zhi_xie\nodejs\neomovie\node_modules\cheerio\lib
\parse.js:17:11)
    at Function.exports.load (C:\Users\zhi_xie\nodejs\neomovie\node_modules\chee
rio\lib\static.js:19:14)
    at C:\Users\zhi_xie\nodejs\neomovie\routes\crawlerusers.js:35:27
    at Request.callback (C:\Users\zhi_xie\nodejs\neomovie\node_modules\superagen
t\lib\node\index.js:746:30)
    at Request.<anonymous> (C:\Users\zhi_xie\nodejs\neomovie\node_modules\supera
gent\lib\node\index.js:135:10)
    at Request.emit (events.js:95:17)
    at ClientRequest.<anonymous> (C:\Users\zhi_xie\nodejs\neomovie\node_modules\
superagent\lib\node\index.js:921:12)
    at ClientRequest.emit (events.js:95:17)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (http.js:1692:21)

以下是代码:

  var superagent = require('superagent');
  var cheerio = require('cheerio');
  var url = require('url');
  var kijijiUrl = 'http://www.kijiji.ca/b-immobilier/ville-de-montreal/c34l1700281?ad=wanted';

  superagent.get(kijijiUrl).end(function (err, res) {
    if (err) {
      return console.error(err);
    }
    var topicUrls = [];
    var $ = cheerio.load(res.text);
    // get all links in that page
    $('div.container-results').children('table').each(function (idx, element) {
      var $element = $(element);
      var href = url.resolve(kijijiUrl, $element.attr('data-vip-url'));
      topicUrls.push(href);
    });

    for(i = 0; i<topicUrls.length; i++){
      superagent.get(topicUrls[i])
        .end(function (err, sres) {
          //normal err handler
          if (err) {
            return next(err);
          }
          var $ = cheerio.load(sres.text);
          var object = {};
          $('table.ad-attributes').find('tr').not('.divider').each(function(i, element){
            var titles = $(element).children('th').text().trim();
            var values = $(element).children('td').text().trim();          
            object[titles] = values;
          });
          console.log(object);               
      });        
    }
  })  

2 个答案:

答案 0 :(得分:6)

正如chrki已经说过的那样,cheerio.load()被调用一个空值,如null或空字符串。在致电.load()之前检查价值是否包含某些内容:

if (res.text) {
    var $ = cheerio.load(res.text);
} else {
    // do something else
}

答案 1 :(得分:1)

我有与

相同的错误消息
r.db("mydb").table("mytable").getAll("a", {index: 'AssetID'}).distinct({index:"StartedOn"})
e: Can only perform an indexed distinct on a TABLE in:

应该是

$ = cheerio.load(response.Body);

所以没有什么能让cheerio加载。