我想将该数据保存到一个 json 文件中,但首先我想将标题、发布日期、评级和评论保存到 movieData 对象,但它给了我错误:movieData 未定义。
var Nightmare = require('nightmare');
var fs = require('fs');
var nightmare = Nightmare({ show: true });
var movieData = {};
nightmare
.goto('http://www.imdb.com/')
.type('#navbar-query', 'ghostbusters')
.click('#navbar-submit-button')
.wait('#main')
.click('.findSection table .findResult a')
.wait('#wrapper')
.evaluate(function () {
movieData.title = document.querySelector('.title_wrapper h1').innerText;
movieData.releaseDate = document.querySelector('.subtext a[title*="dates" i]').innerText;
movieData.rating = document.querySelector('span[itemprop="ratingValue"]').innerText;
return movieData;
})
.then(function (data) {
console.log('Data = ' + JSON.stringify(data));
return nightmare
.click('.user-comments .see-more a:nth-child(3)')
.wait()
.select('select[name="filter"]', 'chrono')
.wait('#wrapper')
.evaluate(function () {
movieData.review = document.querySelector('#tn15content p').innerText;
return movieData;
})
.end()
.then(function (data) {
console.log('Review = ' + data.review);
})
})
.catch(function (error) {
console.error('Search failed:', error);
});