簡體   English   中英

未定義的NodeJS搜尋器不是函數

[英]NodeJS crawler undefined is not a function

我正在對NodeJ使用搜尋器

這是我的代碼:

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/');

但運行了一段時間后,出現此錯誤:

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

問題在這條線上:

$('a').each(function(index, a) {

您正在接近,沒有一個頁面a標簽,所以jQuery對象是空的,且不能有它運行的功能。 在運行每個函數之前,必須檢查以確保其不為空。

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嗎? 它可能在選擇器上引發錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM