是否有可能在服务器端使用Node.js使用jQuery选择器/DOM操作?
当前回答
我手动简单地做了,没有任何额外的包或代码。
npm i jquery
然后我复制jquery.min.js文件从node_modules/jquery/dist目录到public/js
<script type='text/javascript' src='/js/jquery.min.js'></script>
<script>
$(document).ready(function() { console.log( "ready!" ); });
</script>
它会起作用的。测试它
注意,复制/粘贴文件并不是最理想的事情,你可以将文件启用为静态文件,这样expressJS就可以读取它了。但对我来说,把它复制到静态公共目录更容易。
其他回答
首先,安装它
npm install jquery -S
安装后,您可以如下所示使用它
import $ from 'jquery';
window.jQuery = window.$ = $;
$(selector).hide();
你可以查看我写的完整教程:https://medium.com/fbdevclagos/how-to-use-jquery-on-node-df731bd6abc7
据我所知没有。DOM是一个客户端的东西(jQuery不解析HTML,但解析DOM)。
下面是一些当前的Node.js项目:
https://github.com/ry/node/wiki (https://github.com/nodejs/node)
SimonW的djangode非常酷…
这些解决方案在我的电子应用程序中没有一个对我有帮助。
我的解决方案(变通方案):
npm install jquery
在index.js文件中:
var jQuery = $ = require('jquery');
在你的.js文件中,以这样的方式编写jQuery函数:
jQuery(document).ready(function() {
一个使用Cheerio的简单爬虫程序
这是我在Node.js中制作一个简单爬虫程序的公式。这是想要在服务器端进行DOM操作的主要原因,也可能是这里的原因。
首先,使用request下载要解析的页面。下载完成后,将其处理为cheerio,并像使用jQuery一样开始DOM操作。
工作的例子:
var
request = require('request'),
cheerio = require('cheerio');
function parse(url) {
request(url, function (error, response, body) {
var
$ = cheerio.load(body);
$('.question-summary .question-hyperlink').each(function () {
console.info($(this).text());
});
})
}
parse('http://stackoverflow.com/');
这个例子将打印到控制台的所有顶级问题显示在SO主页。这就是为什么我喜欢Node.js和它的社区。没有比这更容易的了:-)
安装的依赖关系:
NPM安装请求
然后运行(假设上面的脚本在爬虫.js文件中):
节点crawler.js
编码
有些页面会有某种编码的非英语内容,您需要将其解码为UTF-8。例如,巴西葡萄牙语(或任何其他起源于拉丁语的语言)的页面可能会被编码为ISO-8859-1(也就是ISO-8859-1)。“latin1”中的一个)。当需要解码时,我告诉request不要以任何方式解释内容,而是使用iconv-lite来完成工作。
工作的例子:
var
request = require('request'),
iconv = require('iconv-lite'),
cheerio = require('cheerio');
var
PAGE_ENCODING = 'utf-8'; // change to match page encoding
function parse(url) {
request({
url: url,
encoding: null // do not interpret content yet
}, function (error, response, body) {
var
$ = cheerio.load(iconv.decode(body, PAGE_ENCODING));
$('.question-summary .question-hyperlink').each(function () {
console.info($(this).text());
});
})
}
parse('http://stackoverflow.com/');
运行之前,安装依赖项:
NPM安装请求iconv-lite
最后:
节点crawler.js
下面的链接
下一步是跟踪链接。假设你想在SO上列出每个顶级问题的所有海报。您必须首先列出所有主要问题(如上例),然后进入每个链接,解析每个问题的页面以获得涉及的用户列表。
当您开始跟踪链接时,回调地狱就开始了。为了避免这种情况,你应该使用一些承诺,未来或其他东西。我总是在我的工具带中保持异步。下面是一个使用async的爬虫的完整示例:
var
url = require('url'),
request = require('request'),
async = require('async'),
cheerio = require('cheerio');
var
baseUrl = 'http://stackoverflow.com/';
// Gets a page and returns a callback with a $ object
function getPage(url, parseFn) {
request({
url: url
}, function (error, response, body) {
parseFn(cheerio.load(body))
});
}
getPage(baseUrl, function ($) {
var
questions;
// Get list of questions
questions = $('.question-summary .question-hyperlink').map(function () {
return {
title: $(this).text(),
url: url.resolve(baseUrl, $(this).attr('href'))
};
}).get().slice(0, 5); // limit to the top 5 questions
// For each question
async.map(questions, function (question, questionDone) {
getPage(question.url, function ($$) {
// Get list of users
question.users = $$('.post-signature .user-details a').map(function () {
return $$(this).text();
}).get();
questionDone(null, question);
});
}, function (err, questionsWithPosters) {
// This function is called by async when all questions have been parsed
questionsWithPosters.forEach(function (question) {
// Prints each question along with its user list
console.info(question.title);
question.users.forEach(function (user) {
console.info('\t%s', user);
});
});
});
});
跑步前:
NPM安装请求异步
运行一个测试:
节点crawler.js
样例输出:
Is it possible to pause a Docker image build?
conradk
Thomasleveil
PHP Image Crop Issue
Elyor
Houston Molinar
Add two object in rails
user1670773
Makoto
max
Asymmetric encryption discrepancy - Android vs Java
Cookie Monster
Wand Maker
Objective-C: Adding 10 seconds to timer in SpriteKit
Christian K Rider
这是你应该知道的基本开始制作你自己的爬虫:-)
库的使用
请求 iconv-lite 恭喜恭喜 异步
另一种选择是使用Underscore.js。它应该提供您可能需要的JQuery服务器端功能。
推荐文章
- 我如何等待一个承诺完成之前返回一个函数的变量?
- CALL_AND_RETRY_LAST分配失败-进程内存不足
- 在JavaScript中根据键值查找和删除数组中的对象
- 使嵌套JavaScript对象平放/不平放的最快方法
- 在Ubuntu上安装Node.js
- 如何以及为什么'a'['toUpperCase']()在JavaScript工作?
- 有Grunt生成index.html不同的设置
- 文档之间的区别。addEventListener和window。addEventListener?
- 如何检查动态附加的事件监听器是否存在?
- 使用express.js代理
- 防止在ASP中缓存。NET MVC中使用属性的特定操作
- 如何写setTimeout与参数Coffeescript
- 将JavaScript字符串中的多个空格替换为单个空格
- jQuery: keyPress退格键不火?
- XMLHttpRequest Origin null不允许Access-Control-Allow-Origin for file:/// to file:///(无服务器)