i want save data json file first want save title,release date, rating , review moviedata object gives me error: moviedata not defined.
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); });
your issue 1 of scope. moviedata
in second evaluate statement not work becuase in different scope compared original moviedata
object literal in first evaluate
function. pass scope, need make changes below.
nightmare .goto('http://www.imdb.com/') .type('#navbar-query', 'ghostbusters') .click('#navbar-submit-button') .wait('#main') .click('.findsection table .findresult a') .wait('#wrapper') // .evaluate(data1()) .evaluate(function () { var moviedata = {} 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 (b) { b.review = document.queryselector('#tn15content p').innertext; return b; }, data) .end() .then(function (data) { console.log('review = ' + data.review); console.log(data); }) }) .catch(function (error) { console.error('search failed:', error); });
this works because passing object moviedata
first then()
function second then()
, passing third scope .evaluate()
through b
.
Comments
Post a Comment