使用服务器端Javascript编写脚本 [英] Scripting with Server-side Javascript
问题描述
什么是良好的服务器端javascript实现,用于编写一次性脚本来处理某些任务或编写要反复使用的自动化脚本。
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.
我对SSJS轻松抓取网页的能力很感兴趣,并且我认为SSJS可以替代Python来满足我的通用脚本需求。是否有这样的SSJS实现?
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?
推荐答案
如果你熟悉jQuery,那么 node.js (使用插件request,jsdom和jquery的端口)让你只需几行就可以轻松地使用jQuery抓取网页。
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());
});
});
或者如果你在浏览器中使用另一个javascript框架,创建自己的MooTools端口并不难,使用jsdom for node.js的原型或其他任何东西(只需要包装任何库来提供 window
, document
和其他全局变量 - jsdom
允许您访问)。
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).
这篇关于使用服务器端Javascript编写脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!