首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未定义的NodeJS爬虫不是一个函数

未定义的NodeJS爬虫不是一个函数
EN

Stack Overflow用户
提问于 2014-11-15 18:20:04
回答 1查看 1.3K关注 0票数 0

我用爬虫做NodeJs

这是我的密码:

代码语言:javascript
复制
var Crawler = require("crawler");
//var jsdom = require('jsdom');
var url = require('url');
var fs = require('fs');

if (typeof String.prototype.startsWith != 'function') {
  // see below for better implementation!
  console.log("added");
  String.prototype.startsWith = function (str){
    return this.indexOf(str) == 0;
  };
}

var c = new Crawler({
    maxConnections: 10,

    // This will be called for each crawled page
    callback: function (error, result,$) {
        // $ is Cheerio by default
        //a lean implementation of core jQuery designed specifically for the server
        if(result.request.uri.href.startsWith("http://www.geocaching.com/geocache/")){
            var titel = $('#ctl00_ContentBody_CacheName');
            var coords = $('#uxLatLon');

            console.log(titel +": "+ coords);
        }
        $('a').each(function(index, a) {
            var toQueueUrl = $(a).attr('href');
            c.queue(toQueueUrl);
        });

    }
});

c.queue('http://www.geocaching.com/');

但在运行了一段时间之后,我得到了以下错误:

代码语言:javascript
复制
TypeError: undefined is not a function
    at Object.Crawler.callback (C:\Users\Lukas\Documents\Geocachcrawler\app.js:27:9)
    at Crawler._onContent (C:\Users\Lukas\Documents\Geocachcrawler\node_modules\crawler\lib\crawler.js:462:17)
    at Request._callback (C:\Users\Lukas\Documents\Geocachcrawler\node_modules\crawler\lib\crawler.js:352:18)
    at Request.self.callback (C:\Users\Lukas\Documents\Geocachcrawler\node_modules\crawler\node_modules\request\request.js:236:22)
    at Request.emit (events.js:98:17)
    at Request.<anonymous> (C:\Users\Lukas\Documents\Geocachcrawler\node_modules\crawler\node_modules\request\request.js:1142:14)
    at Request.emit (events.js:117:20)
    at IncomingMessage.<anonymous> (C:\Users\Lukas\Documents\Geocachcrawler\node_modules\crawler\node_modules\request\request.js:1096:12)
    at IncomingMessage.emit (events.js:117:20)
    at _stream_readable.js:943:16
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-15 18:28:36

问题就在这条线上:

代码语言:javascript
复制
$('a').each(function(index, a) {

您到达的页面没有a标记,因此jQuery对象是空的,不能在其上运行函数。在运行每个函数之前,您必须检查以确保它不是空的。

代码语言:javascript
复制
var a = $('a');
if(a.length != 0){
  $('a').each(function(index, a) {
        var toQueueUrl = $(a).attr('href');
        c.queue(toQueueUrl);
    });
}

更新:我可能不正确,JSfiddle不会在jQuery 1.11.0中抛出此错误。您使用的是什么版本的jQuery?

编辑:您确定包含了jQuery吗?它可能会在选择器上抛出错误。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26949140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档