0
刮這是我目前有:需要一些幫助CasperJS
var casper = require('casper').create();
var fs = require('fs');
var folderName = 'CARD_DATA';
var fileName = 'championDecks.txt';
var save = fs.pathJoin(fs.workingDirectory, folderName, fileName);
// init jquery
var casper = require('casper').create({
clientScripts: ['jquery.min.js']
});
// parse URL
var parseUrl = 'http://magic.wizards.com/en/events/coverage/mtgochamp14';
// scrape
function getDeckData() {
var meta = $('.deck-meta h4').text();
var event = $('.deck-meta h5').text().trim();
return [meta, event];
}
casper.start(parseUrl, function() {
var data = this.evaluate(getDeckData);
fs.write(save, data + '\n', 'w');
});
casper.run();
我想要的格式類似於這樣湊http://magic.wizards.com/en/events/coverage/mtgochamp14:
{
"event": "2014 Magic Online Championship",
"deckName": "(Vintage) Magnus Lantto's Pyromancer Control",
"deck": [
"1 Dack Fayden",
"3 Snapcaster Mage",
"4 Young Pyromancer",
"3 Cabal Therapy",
"1 Demonic Tutor",
"4 Gitaxian Probe",
"1 Ponder",
"3 Preordain",
"1 Time Walk",
"1 Treasure Cruise",
"1 Ancestral Recall",
"1 Brainstorm",
"3 Dig Through Time",
"4 Force of Will",
"3 Gush",
"3 Lightning Bolt",
"4 Mental Misstep",
"1 Pyroblast",
"1 Black Lotus",
"1 Mox Jet",
"1 Mox Ruby",
"1 Mox Sapphire",
"3 Flooded Strand",
"1 Island",
"4 Scalding Tarn",
"1 Strip Mine",
"2 Underground Sea",
"3 Volcanic Island"
],
"sideboard": [
"1 Pyroblast",
"1 Dread of Night",
"1 Electrickery",
"4 Grafdigger's Cage",
"4 Ingot Chewer",
"1 Mountain",
"1 Null Rod",
"2 Pulverize"
],
"event": "2014 Magic Online Championship",
"deckName": "(MODERN) MAGNUS LANTTO'S ELF COMPANY",
"deck": [ ... ],
"sideboard": [ ... ]
...and so on...
}
我不能似乎弄清楚如何單獨獲取數據。這是我目前得到的:
(Vintage) Magnus Lantto's Pyromancer Control(Modern) Magnus Lantto's Elf Company(Standard) Magnus Lantto's Atarka Devotion(Vintage) Jasper de Jong's Mentor Control(Modern) Jasper de Jong's Melira and Company(Standard) Jasper de Jong's Green-White Devotion(Vintage) Aleksa Telarov's Delver(Modern) Aleksa Telarov's Burn(Standard) Aleksa Telarov's Jund Megamorph(Vintage) Antonio Del Moral León's Omni-Oath(Modern) Antonio Del Moral León's Splinter Twin(Standard) Antonio Del Moral León's Abzan Midrange,2014 Magic Online Championship
2014 Magic Online Championship
2014 Magic Online Championship
2014 Magic Online Championship
2014 Magic Online Championship
2014 Magic Online Championship
2014 Magic Online Championship
2014 Magic Online Championship
2014 Magic Online Championship
2014 Magic Online Championship
2014 Magic Online Championship
2014 Magic Online Championship
任何人都可以提供一個小智慧,讓我在正確的方向嗎?
不得不改變我的評論。也許你需要在返回之前在輸出中使用JSON.stringify。也可以嘗試'console.log(output)'來驗證它是否正確填充。 –
這就是我所需要的。謝謝! :) – rotaercz
我沒有改變輸出到JSON.stringify(輸出); – rotaercz