selain dengan x-ray kita juga bisa menggunakan cheerio untuk scraping web, tetapi dengan cheerio tidak sebagus dan selengkap x-ray fiturnya.
npm init
touch index.js
npm i --save cheerio request restify restify-cors-middleware
code .
const restify = require('restify');
const cheerio = require('cheerio');
const request = require('request');
const corsMiddleware = require('restify-cors-middleware');
var URI = "https://www.gsmarena.com";
const cors = corsMiddleware({
preflightMaxAge: 5, //Optional
origins: ['*'],
})
// Get All Phone Brands Maker from GSM ARENA
const phoneBrands = (req, res, next) => {
request({
url: URI + '/makers.php3',
headers: {
"User-Agent": "request"
}
}, (error, response, html) => {
if (!error) {
$ = cheerio.load(html)
let json = [];
let brands = $('table').find('td')
brands.each((i, el) => {
let brand = {
name: $(el).find('a').text().replace(' devices', '').replace(/[0-9]/g, ""),
devices: $(el).find('span').text().replace(' devices', ''),
url: $(el).find('a').attr('href')
}
json.push(brand)
})
res.send(json);
next();
} else {
res.send({
error: "Error failed fetching source"
})
next()
}
})
};
var server = restify.createServer();
server.pre(cors.preflight);
server.use(cors.actual);
server.get('/gsmarena/brands', phoneBrands);
server.head('/gsmarena/brands', phoneBrands);
server.listen(8888, function () {
console.log('%s listening at %s', server.name, server.url);
});
jalankan dengan nodemon/node/pm2 atau forever. selengkapnya silahkan baca https://cheerio.js.org/, https://github.com/request/request dan http://restify.com. Selamat mumet.