下面的代码可以与 NightmareJS 和 NodeJS 一起使用。但是我不想仅仅将我的 JSON 结果输出到控制台,而是想将其保存到本地文件中。
我知道在 Nightmare 中有 .html 选项,但我不想要整个页面,我需要它以 JSON 格式保存。请问如何将 ticketData 保存到本地文件中?
nightmare
.goto('https://www.stubhub.com/')
.wait(3000)
.type('.inputbox', 'chicago bulls')
.mouseover('.search-action-button')
.click('.search-action-button')
.wait(3000)
.evaluate(function () {
var ticketData = {}
ticketData.team = ([].slice.call(document.querySelectorAll('.event-title'))).map( function(x) {
return x.innerText
});
ticketData.date = ([].slice.call(document.querySelectorAll('.date-stamp-month-dd'))).map( function(x) {
return x.innerText
});
ticketData.price = ([].slice.call(document.querySelectorAll('.price'))).map( function(x) {
return x.innerText
});
ticketData.url = ([].slice.call(document.querySelectorAll('.event-link'))).map( function(x) {
return x.href
});
return ticketData;
})
.then(function (data) {
console.log('Data = ' + JSON.stringify(data));
return nightmare
})
.catch(function (error) {
console.error('', error);
});