html – How to fix the CORS error in Node.JS? (Web Crawler)

Well, me and my group of CBT we make a site, where his function is search products on various websites specialized in Equipment and show which site has the lowest price. We make the site with HTML, PHP, JavaScript and Node.JS (who has the modules request and Cheerleader to perform the scan, and Browserify to run the modules in the browser), running only on USBWebserver. It may sound strange, but it works.

That's the code "root"without displaying in HTML, which shows the results of all sites successfully (I've put on show in the console that the amount of information from each site):








var request = require (& # 39; request & # 39;);
var cheerio = require (& # 39; cheerio & # 39;);

search = "processor";

urlKabum = "https://www.kabum.com.br/cgi-local/site/listagem/listagem.cgi?string=" + search;
urlTera = "https://www.terabyteshop.com.br/busca?str=" + search;
urlChip = "https://www.chipart.com.br/produtos/" + search;
urlPichau = "https://www.pichau.com.br/catalogsearch/result/?q=" + search;
urlSubmarino = "https://www.submarino.com.br/busca/" + search;

console.log (urlTera + '+ urlKabum +' + urlChip + '+ urlPichau +' # 39;  n & # 39;  n & # 39; + urlSubmarine);

Kabum function () {
request (urlKabum, function (err, res, html) {
if (err) console.log (& # 39; Error: & # 39; + err);

var $ = cheerio.load (html);

let productoKabum = [];
leave the title = [];
leave the price = [];
leave image = [];

$ (& # 39; .listagem-box & # 39;). each (function (i, elem) {
$ (& # 39; .H-title a & # 39;). Each (function (i, elem) {
title[i] = $ (this) .text ();
});

$ (& # 39; & # 39; price list & # 39;). each (function (i, elem) {
price[i] = $ (this) .text ();
});

$ (& # 39; .list-img img & # 39;). each (function (i, elem) {
picture[i] = $ (this) .attr (& # 39; src & # 39;);
});

titulo.join (& # 39;);
preco.join (& # 39;);
image.join (& # 39;);
productKabum.join (& # 39;);

produitKabum[i] =

title[titulo[titulo[i], price[i]Photo[i]];
});
console.log (productKabum.length);
});
}

Tera function () {
request (urlTera, function (err, res, html) {
if (err) console.log (& # 39; Error: & # 39; + err);

var $ = cheerio.load (html);

let productoTera = [];
leave the title = [];
leave the price = [];
leave image = [];

$ (& # 39; .pbox.col-xs-12.col-sm-6.col-md-3 & # 39;). each (function (i, elem) {
$ (& # 39; .prod-name strong & # 39;). each (function (i, elem) {
title[i] = $ (this) .text ();
});

$ (& # 39; .prod-new-price span & # 39;). each (function (i, elem) {
price[i] = $ (this) .text ();
});

$ (& # 39; .commerce_columns_item_image img & # 39;). each (function (i, elem) {
picture[i] = $ (this) .attr (& # 39; src & # 39;);
});

titulo.join (& # 39;);
preco.join (& # 39;);
image.join (& # 39;);
productT.Join (& # 39;, & # 39;);

if (price[i] == undefined) {
price[i] = & # 39; INDISPONIVE & # 39 ;;
}

product[i] =

title[titulo[titulo[i], price[i]Photo[i]];
});
console.log (productTer.length);
});
}

function ChipArt () {
request (urlChip, function (err, res, html) {
if (err) console.log (& # 39; Error: & # 39; + err);

var $ = cheerio.load (html);

let productoChip = [];
leave the title = [];
leave the price = [];
leave image = [];

$ (& # 39; .products__list__item & # 39;). each (function (i, elem) {
$ (& # 39; .product-card__title & # 39;). each (function (i, elem) {
title[i] = $ (this) .text ();
});

$ (& # 39; .price). each (function (i, elem) {
price[i] = $ (this) .text ();
});

$ (& # 39; .product-card__figure img & # 39;). each (function (i, elem) {
picture[i] = $ (this) .attr (& # 39; src & # 39;);
});

titulo.join (& # 39;);
preco.join (& # 39;);
image.join (& # 39;);
productChip.join (& # 39;);

productChip[i] =

title[titulo[titulo[i], price[i]Photo[i]];
});
console.log (productChip.length);
});
}

Pichau function () {
request (urlPichau, function (err, res, html) {
if (err) console.log (& # 39; Error: & # 39; + err);

var $ = cheerio.load (html);

let productPichau = [];
leave the title = [];
leave the price = [];
leave image = [];

$ (& # 39; .item.product.product-item & # 39;). each (function (i, elem) {
$ (& # 39; .product-item-link & # 39;). each (function (i, elem) {
title[i] = $ (this) .text ();
});

$ (.Price-boleto span & # 39;). each (function (i, elem) {
price[i] = $ (this) .text ();
});

$ (& # 39; .product-image-photo & # 39;). each (function (i, elem) {
picture[i] = $ (this) .attr (& # 39; src & # 39;);
});

titulo.join (& # 39;);
preco.join (& # 39;);
image.join (& # 39;);
productPichau.join (& # 39;);

productPichau[i] =

title[titulo[titulo[i], price[i]Photo[i]];
});
console.log (productPichau.length);
});
}

Underwater function () {
request (urlSubmarine, function (err, res, html) {
if (err) console.log (& # 39; Error: & # 39; + err);

var $ = cheerio.load (html);

leave the product underwater = [];
leave the title = [];
leave the price = [];
leave image = [];

$ (& # 39; .product-grid-item.ColUI-sc-1ey7nd2-0.fUgyk.ViewUI-oocyw8-6.kvewNe & # 39;). each (function (i, elem) {
$ (& # 39; .TitleWrapper-c6mv26-7.cXTBDO.ViewUI-oocyw8-6.kvewNe h1 & # 39;). Each (function (i, elem) {
title[i] = $ (this) .text ();
});

$ (& # 39; .PriceUI-sc-1m3ayw0-10.pYYOk.PriceUI-pftkg3-0.hDaJyt.TextUI-sc-1hrwx40-0.hbVZKK & # 39;). Each (function (i, elem) {
price[i] = $ (this) .text ();
});

$ (& # 39; .ImageUI-sc-1m3ayw0-13.bqjwSN.PictureUI-alry8x-1.fyrQZb img & # 39;). Each (function (i, elem) {
picture[i] = $ (this) .attr (& # 39; src & # 39;);
});

titulo.join (& # 39;);
preco.join (& # 39;);
image.join (& # 39;);
productSubmarine.join (& # 39;);

produitSubmarine[i] =

title[titulo[titulo[i], price[i]Photo[i]];
});
console.log (productSubmarino.length);
});
}

Tera ();
Kabum ();
ChipArt ();
Pichau ();
Submarine ();

However, when I pass the code to HTML, only sites Kabum! and TeraByte could be extracted and posted on our site, and the rest, causing the following error:

Access to extraction at the address & # 39; https: //www.chipart.com/products/processor' from the beginning http: // localhost: 8080 & # 39; was blocked by the CORS strategy: No, header – Access-Control-Allow-Origin-Origin & # 39; is present on the requested resource. If an opaque response meets your needs, set the request mode to "No-cors" to retrieve the resource with CORS disabled.

The following code is a JavaScript at to show on the site (fields like htmlTera and similar fields have empty fields, which are actually only divs, classes, pictures, etc.):








single var = require (& # 39; only);
var request = require (& # 39; request & # 39;);
var cheerio = require (& # 39; cheerio & # 39;);

clean function () {
document.getElementById ("result"). innerHTML = "";
console.clear ();
}

// Treatment of information:
myTrim function (trim) {
return trim.replace (/ ^  s + |  s + $ / gm, & # 39;);
}

function TrimAcent (trim) {
return trim.replace (/ / g, & # 39; í);
}

// URLs of sites to request:
urls function () {
var search = document.getElementById ("inputProduct"). value;

resultSub = search.replace (/ / g, "-");
search = search.replace (/ / g, "+");

urlChip = "https://www.chipart.com.br/produtos/" + search;

urlTera = "https://www.terabyteshop.com.br/busca?str=" + search;
console.log (urlTera);

if ((search == "processor") || (search == "video card")) {
urlKabum = "https://www.kabum.com.br/cgi-local/site/listagem/listagem.cgi?string=" + search;
}

if (search == "ssd") {
urlKabum = "https://www.kabum.com.br/hardware/ssd-2-5/" + search;
}

if (search == "hd") {
urlKabum = "https://www.kabum.com.br/hardware/disco-rigido-hd/" + search;
}

if (search == "cooler") {
urlKabum = "https://www.kabum.com.br/hardware/coolers/" + search;
}

console.log (urlKabum);
}

leave hand = function () {
urls ();
clean ();
Kabum ();
Tera ();
ChipArt ();
}

Kabum function () {
request (urlKabum, function (err, res, html) {
if (err) console.log (& # 39; Error: & # 39; + err);

var $ = cheerio.load (html);

let productoKabum = [];
leave the title = [];
leave the price = [];
leave image = [];

$ (& # 39; .listagem-box & # 39;). each (function (i, elem) {
$ (& # 39; .H-title a & # 39;). Each (function (i, elem) {
title[i] = myTrim (TrimAcent ($ (this) .text ()));
});

$ (& # 39; & # 39; price list & # 39;). each (function (i, elem) {
price[i] = myTrim ($ (this) .text ());
});

$ (& # 39; .list-img img & # 39;). each (function (i, elem) {
picture[i] = myTrim ($ (this) .attr (& # 39; src & # 39;));
});

titulo.join (& # 39;);
preco.join (& # 39;);
image.join (& # 39;);
productKabum.join (& # 39;);

var htmlKabum = & # 39; + title[i] + & # 39; & # 39; + price[i] + & # 39; Select & # 39 ;;

document.getElementById ("result"). innerHTML + = htmlKabum;

produitKabum[i] =

title[titulo[titulo[i], price[i]Photo[i]];
});
console.log (productKabum.length);
});
}

Tera function () {
request (urlTera, function (err, res, html) {
if (err) console.log (& # 39; Error: & # 39; + err);

var $ = cheerio.load (html);

let productoTera = [];
leave the title = [];
leave the price = [];
leave image = [];

$ (& # 39; .pbox.col-xs-12.col-sm-6.col-md-3 & # 39;). each (function (i, elem) {
$ (& # 39; .prod-name strong & # 39;). each (function (i, elem) {
title[i] = myTrim ($ (this) .text ());
});

$ (& # 39; .prod-new-price span & # 39;). each (function (i, elem) {
price[i] = myTrim ($ (this) .text ());
});

$ (& # 39; .commerce_columns_item_image img & # 39;). each (function (i, elem) {
picture[i] = myTrim ($ (this) .attr (& # 39; src & # 39;));
});

titulo.join (& # 39;);
preco.join (& # 39;);
image.join (& # 39;);
productT.Join (& # 39;, & # 39;);

if (price[i] == undefined) {
price[i] = & # 39; INDISPONIVE & # 39 ;;
}

var htmlTera = & # 39; + title[i] + & # 39; & # 39; + price[i] + & # 39; Select & # 39 ;;

document.getElementById ("result"). innerHTML + = htmlTera;

product[i] =

title[titulo[titulo[i], price[i]Photo[i]];
});
console.log (productTer.length);
});
}

function ChipArt () {
request (urlChip, function (err, res, html) {
if (err) console.log (& # 39; Error: & # 39; + err);

var $ = cheerio.load (html);

let productoChip = [];
leave the title = [];
leave the price = [];
leave image = [];

$ (& # 39; .products__list__item & # 39;). each (function (i, elem) {
$ (& # 39; .product-card__title & # 39;). each (function (i, elem) {
title[i] = $ (this) .text ();
});

$ (& # 39; .price). each (function (i, elem) {
price[i] = $ (this) .text ();
});

$ (& # 39; .product-card__figure img & # 39;). each (function (i, elem) {
picture[i] = $ (this) .attr (& # 39; src & # 39;);
});

titulo.join (& # 39;);
preco.join (& # 39;);
image.join (& # 39;);
productChip.join (& # 39;);

var htmlChip = & # 39; + title[i] + & # 39; & # 39; + price[i] + & # 39; Select & # 39 ;;

document.getElementById ("result"). innerHTML + = htmlChip;

productChip[i] =

title[titulo[titulo[i], price[i]Photo[i]];
});
console.log (productChip.length);
});
}

let button = document.getElementById ("search");
button.addEventListener ("click", main, false);

What could cause this error?

insert the description of the image here