[英]Node.js & node.io - iterating elements using “each” function stops server
[英]JS/Node:- Selecting a tag using node.io
我是一個初學者,正在做作業,以使用node.io抓取此頁面的內容
http://www.nycourts.gov/reporter/3dseries/2013/2013_06966.htm 。
我想將<P>標記下的文本內容另存為字符串形式的變量。
我的代碼是這樣的:
var nodeio = require('node.io'); var方法= {輸入:false,運行:function(){this.getHtml(' http://www.nycourts.gov/reporter/3dseries/2013/2013_06966.htm',function (err,$){
//Handle any request / parsing errors if (err) this.exit(err); var content = $('P'); this.emit(content); }); } }
Exports.job =新的nodeio.Job({timeout:10},方法);
這顯示錯誤:沒有元素匹配'P'。 請幫忙..
我得到Error: No elements matching 'P'
執行命令時也Error: No elements matching 'P'
:
$ ./node_modules/.bin/node.io query http://www.nycourts.gov/reporter/3dseries/2013/2013_06966.htm P
根本原因是該頁面沒有結束</P>
,並且node.io不支持對格式錯誤的HTML(例如現代Web瀏覽器)進行自動更正。 在查詢<blockquote>
時效果很好:
$ ./node_modules/.bin/node.io query http://www.nycourts.gov/reporter/3dseries/2013/2013_06966.htm blockquote
但是,您可以通過使用硒技術在真實的瀏覽器上解析HTML文檔來實現。
以下示例javascript可以在主機上與node和硒網格一起運行,以獲取所需的內容。 您可以參考我對問題的其他答案。 如何使webdriverjs工作? :
var webdriverjs = require('webdriverjs');
var client = webdriverjs.remote({
host: 'localhost',
port: 4444,
desiredCapabilities: {
browserName: 'safari', // you can change this accordingly
version: '7',
platform: "MAC" // you can change this accordingly
}
});
client.init();
client.url('http://www.nycourts.gov/reporter/3dseries/2013/2013_06966.htm')
.getText("P",function(err, text) { console.log (text)}).call(function () {});
client.end();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.