简体   繁体   中英

How do I write data from NightmareJS to file

I'm new to JavaScript, node.js, and NightmareJS.

I've written a simple script below to extract some text from a webpage, and I would like to save it to a file.

var nightmare = require('nightmare');
var data = [];
var fs = require('fs');

var usda = new nightmare()
.goto('yyyy')
.wait(20000)
.inject('js', 'jquery.js')
.evaluate(function(){  
  data = $x('//a').text();  
  fs.write("testOutput.json", JSON.stringify(data), 'w');
})
.end()
.run(function (err, nightmare) {
    if (err) return console.log(err);
    console.log('Done!');
});

I keep getting the error shown below:

return binding.writeString(fd, buffer, offset, length, req);
             ^
TypeError: First argument must be file descriptor

Function contents inside of .evaluate() are run in the browser context. As such, fs and data won't be lifted into the function scope you've defined. (You can read more about variable lifting and .evaluate() here .)

fs.write() won't work as you intend - fs.write() is asynchronous .

Also, I doubt $(selector).text() is going to yield the results you want - I think that will concatenate the link text from each link together. I suspect you want them in an array?

Furthermore, I should point out that .run() isn't directly supported . It's an internal function, kept around mostly for compatibility.

Finally, it would appear you're using either a custom build of jQuery or a third party library to get XPath support. In the future, it would be helpful to include that information.

All of that said, let's patch up your example to get you started. Off the cuff, something like this should work:

var nightmare = require('nightmare');
var fs = require('fs');

var usda = new nightmare()
.goto('yyyy')
.wait(20000)
.inject('js', 'jquery.js')
.evaluate(function(){
  //using 'a', but this could be swapped for your xpath selector
  return $('a').toArray().map((a) => $(a).text());
})
.end()
.then(function(anchors){
  fs.writeFileSync('testOutput.json', JSON.stringify(anchors));
  console.log('Done!');
});
.catch(function(err){
  console.log(err);
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM