Answer the question
In order to leave comments, you need to log in
Strange behavior of NodeJS+XPath
Good afternoon
I'm trying to learn the now fashionable NodeJS and for this I decided to make myself a service for "gutting" sites using Xpath queries.
Why NodeJS - because I want to learn it
Why Xpath - because I decided not to reinvent the "bicycle"
Why not JQuery - because using JQ you can not get the value of an XML attribute of a node through a request (analogue in Xpath)
Actually the NodeJS code itself//a/href
var xpath = require('xpath')
var dom = require('xmldom').DOMParser
var request = require('request')
var tidy = require('tidy2')
var util = require('util')
var url=['http://www.rul.by/companies/sect.1.html',
'http://www.rul.by/companies/sect.1.page.2.html',
]
url.forEach(function(item){
request(item, function (error, response, body) {
console.log('Fetch url:'+item);
if (!error && response.statusCode == 200) {
var array=new Array();
var html=tidy.tidyString(body)
var doc = new dom().parseFromString(html)
//
var fields=["//li[@class='nm']/a/text()",
"//li[@class='adr']/text()",
"//li[@class='txt']/text()",
"//td[@class='cont']/ul/li/text()",
"//td[@class='cont']/ul/li/a/text()"]
fields.forEach(function(field){
var tmp=xpath.select(field, doc)
console.log(util.inspect(tmp));
});
}
})
}, this)
//title
it returns what you need. Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question