TypeError:未定义不是构造函数(评估“ casper.done()”)错误

时间:2018-06-27 15:18:00

标签: javascript phantomjs casperjs

我尝试通过搜索查询后,从youtube页面上的所有视频链接中抓取href属性值。

var casper = require('casper').create();
var links;

function getLinks() {
    var links = document.querySelectorAll('div#contents div#dismissable a#thumbnail');
    return Array.prototype.map.call(links, function (e) {
        return e.getAttribute('href')
    });
}

casper.start('https://www.youtube.com/results?search_query=a');

casper.then(function () {
    links = this.evaluate(getLinks);
});

casper.run(function () {
    for(var i in links) {
        console.log(links[i]);
    }
    casper.done();
});

也是

 div#contents div#dismissable a#thumbnail 

抓取视频每个标签的href属性的正确路径。

当我运行这段代码时,我得到一个错误提示

  

TypeError:未定义不是构造函数(正在评估'casper.done()')

     

C:/ Users / rohit / Desktop / scraping code / phantomjs:/code/test.js:24

     

C:/ Users / rohit / Desktop / scraping code / phantomjs:/platform/casper.js:423 in CheckStep

2 个答案:

答案 0 :(得分:0)

我不确定是否这行:

casper.then(function () {
    links = this.evaluate(getLinks);
});

应该是

casper.then(function () {
    links = this.evaluate(getLinks());
});

casper.then(function () {
    links = this.evaluate(function(){
        getLinks();
    });
});

答案 1 :(得分:0)

casper.done();已过时,在脚本中不再需要,请参见the issue,只需将其从脚本中删除。

相关问题