Answer the question
In order to leave comments, you need to log in
Parsing a Node.js page?
Good afternoon, I'm just learning to program and decided to write a parser, but there was a problem.
The essence of the parser is to find all phone numbers on the page and display them, but it doesn’t find them correctly, I’ve been suffering for 2 days and I can’t solve the problem.
I think the problem is in the regex.
In general, how to solve the problem?
var request = require("request");
var cheerio = require("cheerio");
var url = "ссылка на сайт";
var words = [];
var totalResults = 0;
function callback (word) {
words.push(word);
console.log(words[totalResults]);
return;
}
request(url, function (error, response, body) {
if (error) {
console.log("Не удалось получить страницу из за следующей ошибки:" + error);
return;
}
var $page = cheerio.load(body);
var text = $page("body").text();
text = text.replace(/\s+/g, " ").replace(/^(\s*)?(\+)?([- _():=+]?\d[- _():=+]?){10,14}(\s*)?$/, "");
text.split(" ").forEach(function(word) {
callback(word);
totalResults++;
});
});
Answer the question
In order to leave comments, you need to log in
var request = require("request");
var cheerio = require("cheerio");
var url = "ссылка на сайт";
var words = [];
var totalResults = 0;
function callback (word) {
words.push(word);
console.log(words[totalResults]);
return;
}
request(url, function (error, response, body) {
if (error) {
console.log("Не удалось получить страницу из за следующей ошибки:" + error);
return;
}
var $page = cheerio.load(body);
var text = $page("body").text();
text = text.replace(/\s+/g,"").replace(/w/g,"").match(/8\((\d{4})\)\d{2}-\d{2}-\d{2}/g);
text.forEach(function(word) {
callback(word);
totalResults++;
});
});
They are not looking for regulars anymore, use selectors or xpatch. In chrome, in devtools, you can easily get the selector.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question