const http = require('http'); const cheerio = require('cheerio'); http.get( 'http://thuoctot24h.com/thuc-pham-chuc-nang/sinh-lysinh-san-nam', function(res) { let rawData = ''; res.on('data', (chunk) => { rawData += chunk; }); res.on('end', async() => { const $ = await cheerio.load(rawData); const selecTenSP = $('div.list-item'); var listURL = []; for (var i = 0; i < selecTenSP.length; i++) { listURL.push(selecTenSP[i].children[0].children[0].children[1].attribs.href); } listURL.forEach(function(motPhanTu) { http.get( motPhanTu, function(res) { let rawData = ''; res.on('data', (chunk) => { rawData += chunk; }); res.on('end', async() => { const $ = await cheerio.load(rawData); //ten thuoc const selecTenSP = $('h1.title-name-product'); var tenSP = selecTenSP[0].children[0].data; //Gia const selecGiaSP = $('span.lb-price.price-main'); var giaSP = selecGiaSP[0].children[0].data; var selec = $('#content > h2'); var motChiMuc=[]; for (var i = 0; i < selec.length; i++) { var line = []; while (selec[i].next.name == 'p') { if (selec[i].next.children[0].data != null) line.push(selec[i].next.children[0].data); selec[i] = selec[i].next; if (selec[i].next == null) break; } motChiMuc.push(line); } //Thanh phan var thanhPhan=[]; const selecTable=$('#content > table'); if(selecTable.name!='table') { console.log("Co bang!"); }else { console.log('Khong co bang'); } // var line=[]; // var motThanhPhan=[]; // while(selecTable[0].name=='tr') // { // for(var i=0;i<4;i++) // { // line.push(selecTable[0].children[i].children[0].data); // if(i==1 || i==3) // { // motThanhPhan.push(line); // line=[]; // } // } // listThanhPhan.push(motThanhPhan); // if(selecTable[0].next==null) // break; // selecTable[0]=selecTable[0].next; // } // thanhPhan.push(motThanhPhan); // }else // { // } var congDung=motChiMuc[0]; var doiTuongSD=motChiMuc[2]; var cachSD=motChiMuc[3]; var nguonGoc=motChiMuc[4]; var data = { ten: tenSP, gia: giaSP, congDung:congDung, doiTuongSD:doiTuongSD, cachSuDung: cachSD, thongTinSP: nguonGoc }; const str = JSON.stringify(data); console.log(str); console.log('Mot san pham-----------'); }) } ) }) }) } )