![](/img/trans.png)
[英]Node.js & node.io - iterating elements using “each” function stops server
[英]How to use node.io for HTML parsing from node.js?
我正在嘗試在node.js
上使用node.io
來解析一個HTML頁面,該頁面作為變量中的字符串。
我在將HTML字符串作為參數傳遞給我的node.io
作業時遇到了麻煩。
這是我的節點文件nodeiotest.js
代碼的摘錄:
var nodeIOJob = require('./nodeiojobfile.js');
var nodeio = require('node.io');
vat htmlString = 'HTML String Here';
nodeio.start(nodeIOJob.job, function(err, output) {
console.log(output);
}, true);
接下來是我的文件nodeiojobfile.js
的摘錄:
var nodeio = require('node.io');
var methods = {
input: ['xxxxxxxxxxxxxxxx'], // htmlString is suppossed to come here
run: function (num) {
console.log(num);
this.emit('Hello World!');
}
}
exports.job = new nodeio.Job(methods);
如何將htmlString
作為參數發送到另一個文件中的作業?
另外,在收到文件后,我需要將其解析為HTML並執行一些基本的CSS選擇(例如getElementById()等),並需要計算某些HTML元素的offsetHeight
。 文檔說我可以使用get()
和getHTML()
方法來解析URL的html,但是字符串中的HTML呢? 如何解析它們?
為了測試目的,我使用他下面的HTML:
<div>
<p id="p1">
Testing document
</p>
</div>
我試圖選擇<p>
,然后找出其高度。
誰能幫我? 提前thnx!
我不熟悉node.io
,但我認為您想要這樣的東西:
// nodeiotest.js
...
var htmlString = 'HTML String Here';
nodeio.start(nodeIOJob.job(htmlString), function(err, output) {
console.log(output);
}, true);
// nodeiojobfile.js
var nodeio = require('node.io');
module.exports.job = function(htmlString) {
var methods = {
input: [ htmlString ],
run : function (num) {
console.log(num);
this.emit('Hello World!');
}
};
return new nodeio.Job(methods);
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.