W
W
Wahid2019-04-10 11:20:23
Regular Expressions
Wahid, 2019-04-10 11:20:23

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

2 answer(s)
W
Wahid, 2019-04-11
@Vakha-MockingBird

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++;            
      });		
});

K
Kirill Kudryavtsev, 2019-04-10
@Deissh

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 question

Ask a Question

731 491 924 answers to any question