Javascript NodeJS - 一次请求太多?

时间:2015-02-25 09:12:56

标签: javascript node.js httprequest

我的请求有问题,因为大约有1-3个请求失控,我不知道为什么。我把两个变量:计数器和检查,它们应该是平等的,但通常它们不是。

如果我对小数据进行检查,例如1服务,它的工作原理,但所有数据都无法正常工作。

var request = require('request');

var UserAgent = 'Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101     Firefox/36.0'

var check = ""
var counter = ""

services = [
{
    name: "otodom",
    url: 'http://otodom.pl/',
    css_selector: '.search__counter',
    sell: {
        individual: {
            house: 'http://otodom.pl/index.php?mod=listing&source=context&objSearchQuery.OfferType=sell&objSearchQuery.OnlyFromRegularUser=y&objSearchQuery.ObjectName=House',
            terrain: 'http://otodom.pl/index.php?mod=listing&source=context&objSearchQuery.OfferType=sell&objSearchQuery.OnlyFromRegularUser=y&objSearchQuery.ObjectName=Terrain',
            flat: 'http://otodom.pl/index.php?mod=listing&source=context&objSearchQuery.OfferType=sell&objSearchQuery.OnlyFromRegularUser=y&objSearchQuery.ObjectName=Flat',
        },
        totalUser: {
            house: 'http://otodom.pl/index.php?mod=listing&source=context&objSearchQuery.OfferType=sell&objSearchQuery.ObjectName=House',
            terrain: 'http://otodom.pl/index.php?mod=listing&source=context&objSearchQuery.OfferType=sell&objSearchQuery.ObjectName=Terrain',
            flat: 'http://otodom.pl/index.php?mod=listing&source=context&objSearchQuery.OfferType=sell&objSearchQuery.ObjectName=Flat',
        }
    },
    rent: {
        individual: {
            house: 'http://otodom.pl/index.php?mod=listing&source=context&objSearchQuery.OfferType=rent&objSearchQuery.OnlyFromRegularUser=y&objSearchQuery.ObjectName=House',
            terrain: 'http://otodom.pl/index.php?mod=listing&source=context&objSearchQuery.OfferType=rent&objSearchQuery.OnlyFromRegularUser=y&objSearchQuery.ObjectName=Terrain',
            flat: 'http://otodom.pl/index.php?mod=listing&source=context&objSearchQuery.OfferType=rent&objSearchQuery.OnlyFromRegularUser=y&objSearchQuery.ObjectName=Flat',
            room: 'http://otodom.pl/index.php?mod=listing&source=context&objSearchQuery.OfferType=rent&objSearchQuery.OnlyFromRegularUser=y&objSearchQuery.ObjectName=Room',
        },
        totalUser: {
            house: 'http://otodom.pl/index.php?mod=listing&source=context&objSearchQuery.OfferType=sell&objSearchQuery.ObjectName=House',
            terrain: 'http://otodom.pl/index.php?mod=listing&source=context&objSearchQuery.OfferType=sell&objSearchQuery.ObjectName=Terrain',
            flat: 'http://otodom.pl/index.php?mod=listing&source=context&objSearchQuery.OfferType=sell&objSearchQuery.ObjectName=Flat',
            room: 'http://otodom.pl/index.php?mod=listing&source=context&objSearchQuery.OfferType=sell&objSearchQuery.ObjectName=Room',
        }
    }
},
{
    name: "olx",
    url: 'http://olx.pl/nieruchomosci/',
    css_selector: '.color-2',
    sell: {
        individual: {
            house: 'http://olx.pl/nieruchomosci/domy/sprzedaz/',
            terrain: 'http://olx.pl/nieruchomosci/dzialki/sprzedaz/',
            flat: 'http://olx.pl/nieruchomosci/mieszkania/sprzedaz/',
        },
        totalUser: {
            house: 'http://olx.pl/nieruchomosci/mieszkania/sprzedaz/',
            terrain: 'http://olx.pl/nieruchomosci/dzialki/sprzedaz/',
            flat: 'http://olx.pl/nieruchomosci/mieszkania/sprzedaz/',
        }
    },
    rent: {
        individual: {
            house: 'http://olx.pl/nieruchomosci/domy/wynajem/',
            terrain: 'http://olx.pl/nieruchomosci/dzialki/wynajem/',
            room: 'http://olx.pl/nieruchomosci/stancje-pokoje/',
            flat: 'http://olx.pl/nieruchomosci/mieszkania/wynajem/',
        },
        totalUser: {
            house: 'http://olx.pl/nieruchomosci/mieszkania/wynajem/',
            terrain: 'http://olx.pl/nieruchomosci/dzialki/wynajem/',
            room: 'http://olx.pl/nieruchomosci/stancje-pokoje/',
            flat: 'http://olx.pl/nieruchomosci/mieszkania/wynajem/',
        }
    }
},
{
    name: "gratka.pl",
    url: 'http://dom.gartka.pl/',
    css_selector: '.ilosc',
    sell: {
        individual: {
            house: 'http://dom.gratka.pl/domy-sprzedam/lista/,,on,zi.html',
            terrain: 'http://dom.gratka.pl/dzialki-grunty-sprzedam/lista/,,on,zi.html',
            flat: 'http://dom.gratka.pl/mieszkania-sprzedam/lista/,,on,zi.html',
        },
        totalUser: {
            house: 'http://dom.gratka.pl/domy-sprzedam/lista/',
            terrain: 'http://dom.gratka.pl/dzialki-grunty-sprzedam/lista/',
            flat: 'http://dom.gratka.pl/mieszkania-sprzedam/lista/',
        }
    },
    rent: {
        individual: {
            house: 'http://dom.gratka.pl/domy-do-wynajecia/lista/,,on,zi.html',
            terrain: 'http://dom.gratka.pl/dzialki-grunty-do-wydzierzawienia/lista/,,on,zi.html',
            room: 'http://dom.gratka.pl/pokoje-do-wynajecia/lista/,,on,zi.html',
            flat: 'http://dom.gratka.pl/mieszkania-do-wynajecia/lista/,,on,zi.html',
        },
        totalUser: {
            house: 'http://dom.gratka.pl/domy-do-wynajecia/lista/',
            terrain: 'http://dom.gratka.pl/dzialki-grunty-do-wydzierzawienia/lista/',
            room: 'http://dom.gratka.pl/pokoje-do-wynajecia/lista/',
            flat: 'http://dom.gratka.pl/mieszkania-do-wynajecia/lista/',
        }
    }
}

这是一个简短的剧本

function do_get (page, category, typ, user, service) {
   counter += '1'
   console.log(counter.length)
   request({url: page, headers: {'User-Agent': UserAgent}}, function(buffer,   res, body) {
     check += '1'
     console.log(check.length)
});

}

function do_that (service) {
    for(var user in service.sell) {
        var users = service.sell[user];
        for (var category in users) {
            do_get(users[category], category, "sell", user, service);
       }
    }
    for(var user in service.rent) {
         var users = service.rent[user];
         for (var category in users) {
            do_get(users[category], category, "rent", user, service);
         }
    }
}

for(var i = 0; i < services.length; i++) {
    do_that(services[i]);
}

0 个答案:

没有答案