我正在研究一个项目,在这个项目中循环访问一系列发票并从每张发票中提取数据表。我正在使用CasperJS编写代码,并且已经达到了可以将所有相关信息删除并将其存储在一个数组中的程度,但是无法将信息输出到结构化的Excel中。CasperJS:将刮取的数据导出到结构化的Excel或CSV
代码工作现在的方法如下:
1)捕获每张发票
2的URL)贯穿于各个环节的循环和捕捉如发票号码,发票日期,每个购买的产品名称关键信息,以及每件产品的价格。
这里是在这部分代码看起来像:
function getDescriptions() {
var description = document.querySelectorAll('#InvoiceDetailGrid tbody tr td:nth-child(3)');
return Array.prototype.map.call(description, function(elem) {
return elem.textContent;
});
}
function getPrices() {
var price = document.querySelectorAll('#InvoiceDetailGrid tbody tr td:nth-child(4)');
return Array.prototype.map.call(price, function(elem) {
return elem.textContent;
});
}
casper.then(function() {
for(var z = 0; z < 5; z++) {
casper.thenOpen(baseURL + links[z]);
this.wait(2000, function() {
invoiceNumber = invoiceNumber.concat(this.fetchText('#InvoiceNumber'));
invoiceDate = invoiceDate.concat(this.fetchText(x('//*[@id="printArea"]/table/tbody/tr[1]/td/table/tbody/tr/td[1]/table/tbody/tr[2]/td[2]')));
description = description.concat(this.evaluate(getDescriptions));
price = price.concat(this.evaluate(getPrices));
});
}
});
输出是正确的,会是这个样子:invoiceNumber = 1,2,3; invoiceDate = 2015年1月1日,2015年1月2日,2015年1月3日;描述=产品X,产品Y,产品X,产品Z,产品A,产品B,产品C;价格= 1美元,2美元,1美元,3美元,4美元,5美元,10美元。我想把这个数据看起来像这样的表:
发票号码发票日期描述价格
1 2015年1月1日产品X $ 1
1 2015年1月1日产品Y $ 2
1月2日2 2015年产品X $ 1
2 2015年1月2日产物Z $ 3
3 2015年1月3日产品A $ 4
3 2015年1月3日产品B $ 5
3 2015年1月3日产品C $ 10
每个invoiceNumber和invoiceDate w应与多种描述和价格相关联,并且我想确保维持这种关系(如表中所示)。谢谢!!
后来在感谢抬起头来!我已更新该帖子;希望它现在更有意义 – 2015-04-05 18:16:06