CasperJS无法抓取AJAX网页

时间:2015-07-11 05:54:36

标签: ajax web-scraping phantomjs casperjs headless-browser

我正在尝试使用AJAX调用来抓取http://www.snapdeal.com/offers/deal-of-the-day加载JSON数据到下面的页面:

json_url = http://www.snapdeal.com/json/getProductById?*

我正在使用的代码块如下,我收到日志消息Waiting for AJAX request:,但不是Waiting for AJAX request:,而是waitForResource超时

casper.options.onResourceRequested = function (casper, requestData){
    // loop through our AJAX urls
    // create list of AJAX urls to track
    var ajaxUrls = [json_url];

    ajaxUrls.every(function(ajaxUrl){
        // does this request match an AJAX url
        if(requestData.url.indexOf(ajaxUrl) !== -1){
            // it matches, so we'll wait for it to return (with 10s timeout)
            //console.log("Waiting for AJAX request: " + requestData.url);
            // print_object(requestData);
            casper.waitForResource(requestData.url, function(){
                console.log("AJAX request returned: " + requestData.url);
            }, function(){
                console.log("AJAX request didn't return after wait period: " + requestData.url)
            }, 10000);
        }
    });
}

为了进一步调试,我记录了事件,并且已成功收到网址json_url的资源,但不确定为什么waitForResource超时。

casper.on('resource.received', function(resource) {
    if (resource.url.indexOf('http://www.snapdeal.com/json/getProductById') != -1){
        casper.echo('resource.received: ' + resource.url);

    }

});

运行后记录:

Waiting for AJAX request: http://www.snapdeal.com/json/getProductById?pogIds=671556289429,649272180,60998,685755068805,677649317861,1239888775,661402031482,636966047361,1775140628,1822452791,439536 ,Range,2042952975,1472100667,899358889,643129681532,668235859588,&lang=en

resource.received: http://www.snapdeal.com/json/getProductById?pogIds=671556289429,649272180,60998,685755068805,677649317861,1239888775,661402031482,636966047361,1775140628,1822452791,439536,Range, 2042952975,1472100667,899358889,643129681532,668235859588,&lang=en

resource.received: http://www.snapdeal.com/json/getProductById?pogIds=671556289429,649272180,60998,685755068805,677649317861,1239888775,661402031482,636966047361,1775140628,1822452791,439536,Range, 2042952975,1472100667,899358889,643129681532,668235859588,&lang=en

AJAX request didn't return after wait period: http://www.snapdeal.com/json/getProductById?pogIds=671556289429,649272180,60998,685755068805,677649317861,1239888775,661402031482,636966047361,17751406 28,1822452791,439536,Range,2042952975,1472100667,899358889,643129681532,668235859588,&lang=en Got page No Offeres Found thestartin: ~/Appli

1 个答案:

答案 0 :(得分:0)

您需要的是:

casper.waitForSelector()

casper.waitFor(function() {
    // test here something that return true when the page is loaded
})