我正在尝试创建一个article
对象构造函数,该构造函数包含文档标题,作者等属性,并根据我传递给它的不同URL使用对象的创建实例。在下面的示例中,this.articleText
应该返回一个字符串数组,但它不返回任何内容。
这与this.headlineText
形成对比,hello
按预期返回字符串document
。
鉴于这种差异,我怀疑问题的根本原因是构造函数中使用的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();
});
对象。但是,输出中没有抛出任何错误,那么我应该如何解决这个问题呢?
任何建议都非常感谢。 (css选择器本身没有问题;如果我使用函数来获取文章字符串,那么这似乎有用。问题是当尝试使用对象来解析数据时)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using Enums;
using DELL.AFP.Management.ProjectGroup;
using Dell.AFP.BizManager;
using Dell.AFP.Entity;
using System.Data;
namespace AFPManagementServiceLibrary.ProjectGroup.Model
{
[DataContract]
public class ProjectGroupInfoResponse
{
public ProjectGroupInfoResponse(Dell.AFP.Entity.ProjectGroup ProjectGroup)
{
if (ProjectGroup != null)
{
CreatedDate = ProjectGroup.CreatedDateTime;
HigestProjectState = (AFPStateEnum)Convert.ToInt32(ProjectGroup.AFPState);
CustomerGroup = (CustomerGroupEnum)ProjectGroup.CustomerGroupId;
ProjectGroupName = ProjectGroup.Title;
ProjectGroupOwner = ProjectGroup.CreatedByUser;
IsRestricted = ProjectGroup.IsPublic;
}
}
[DataMember]
public DateTime CreatedDate { get; set; }
[DataMember]
public AFPStateEnum HigestProjectState { get; set; }
[DataMember]
public CustomerGroupEnum CustomerGroup { get; set; }
[DataMember]
public string ProjectGroupName { get; set; }
[DataMember]
public string ProjectGroupOwner { get; set; }
[DataMember]
public bool IsRestricted { get; set; }
}
}
答案 0 :(得分:0)
对如何访问页面的文档对象存在误解。 您必须使用evaluate,因为所有页面调用都在沙箱中执行:
所以你必须调用evaluate,并从中返回你的querySelectorAll,然后将那个传递给你的函数:)
祝你好运答案 1 :(得分:0)
Whenever you want to run javascript specific code you have to run it using evaluate
function,which are executed in a sandbox.
You want to write something like this
casper.then(function() {
var a = this.evaluate(article(title, url),{title:'hello',url:'url'});
});