问题
What is a good server-side javascript implementation for writing both one-off scripts to handle some task or writing automation scripts to be used over and over.
I am intrigued by the ability for SSJS to scrape webpages with such ease and am thinking SSJS could replace Python for my generic scripting needs. Is there a SSJS implementation for such things?
回答1:
If you're familiar with jQuery, then node.js (with the plugins "request", "jsdom", and a port of jquery) let's you easily scrape web pages using jQuery in only a few lines.
The below will print a list of the all the questions on stack overflow's homepage to your console:
// Importing required modules
var request = require("request"),
$ = require("jquery");
request({uri: "http://www.stackoverflow.com/"}, function (err, response, body) {
$(body).find("#question-mini-list h3 a").each(function () {
console.log($(this).text());
});
});
Or if you use another javascript framework in the browser, it's not hard creating your own port of MooTools, Prototype or whatever using jsdom for node.js (it's just a matter of wrapping whatever library to provide it with window
, document
and other global variables - which jsdom
gives you access to).
回答2:
I'm a fan of node.js. Although its main strength is in building servers (which is apparently not your intention) it is sufficiently versatile and definately worth a look.
回答3:
I've had good results with Rhino + Quartz
来源:https://stackoverflow.com/questions/4393757/scripting-with-server-side-javascript