【问题标题】:TypeError: undefined is not a constructor (evaluating 'casper.done()') errorTypeError: undefined is not a constructor (evalating 'casper.done()') 错误
【发布时间】:2018-06-27 15:18:00
【问题描述】:

通过搜索查询后,我试图从 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 

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

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

TypeError: undefined is not a constructor (evalating 'casper.done()')

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

C:/Users/rohit/Desktop/scraping codes/phantomjs:/platform/casper.js:423 in checkStep

【问题讨论】:

    标签: javascript phantomjs casperjs


    【解决方案1】:

    我不确定这条线是否:

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

    应该是

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

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

    【讨论】:

    • 不幸的是,你错了——这是传递函数以供以后执行的方式。 this.evaluate(getLinks()); 立即执行 getLinks
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-15
      • 2018-07-14
      • 2020-08-27
      • 2022-01-09
      • 1970-01-01
      • 2014-05-05
      • 1970-01-01
      相关资源
      最近更新 更多