【发布时间】:2015-06-26 18:30:43
【问题描述】:
我正在尝试创建一个 article 对象构造函数,该构造函数包含文档标题、作者等属性,并根据我传递给它的不同 URL 使用创建对象实例。在下面的示例中,this.articleText 应该返回一个字符串数组,但它什么也不返回。
这与this.headlineText 形成对比,后者按预期返回字符串hello。
鉴于这种差异,我怀疑问题的根本原因是构造函数中使用了document 对象。但是,输出中没有抛出任何错误,那么我应该如何解决这个问题?
非常感谢任何建议。 (css选择器本身没有问题;如果我使用函数来获取文章字符串,那么这似乎可行。问题在于尝试使用对象解析数据时)
var fs = require('fs');
var casper = require("casper").create({
verbose: true,
logLevel: "debug"
});
function article(title, url) {
this.headlineText = title;
this.urlString = url;
var query = document.querySelectorAll("[itemprop='articleBody']");
this.articleText = Array.prototype.map.call(query, function (e) {
return e.innerText;
});
}
casper.start("http://www.yomiuri.co.jp/economy/20150625-OYT1T50136.html" , function() {
this.test.assertExists({
type: 'css',
path: '[itemprop="articleBody"]'
}, 'Article Exists');
});
casper.run(function() {
var test1 = new article("hello","http://www.yomiuri.co.jp/economy/20150625-OYT1T50136.html");
console.log("HEADLINE==");
console.log(test1.headlineText); // returns "hello"
console.log("ARTICLE == ");
console.log(test1.articleText); // returns nothing
this.exit();
});
【问题讨论】:
标签: javascript casperjs