簡體   English   中英

如何使用node.io從node.js進行HTML解析?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM