Change url web-scraped

This commit is contained in:
ttD4T 2023-06-13 22:23:57 +07:00
parent aa2da5ecbd
commit 6040461666

View File

@ -1,128 +1,135 @@
const axios = require('axios'); const axios = require('axios');
const cheerio = require('cheerio'); const cheerio = require('cheerio');
const url = 'https://xosoketqua.com/xsmb-xo-so-mien-bac.html'; // const url = 'https://xosoketqua.com/xsmb-xo-so-mien-bac.html';
const url = 'https://az24.vn/xsmb-sxmb-xo-so-mien-bac.html';
class MainController { class MainController {
getAll = async (req, res) => { getAll = async (req, res) => {
const numbers = []; const numbers = [];
const names = []; const names = [];
const times = []; const results = {};
const objTimesNames = {};
const obj = {};
try { try {
await axios(url).then(response => { axios(url, {
const html = response.data; method: 'GET',
const $ = cheerio.load(html); // sử dụng giống jQuery })
.then((response) => {
const html = response.data;
const $ = cheerio.load(html); // sử dụng giống jQuery
$('table:nth-child(1)', html) $('table:nth-child(1)', html)
// .first() .first()
.each(function () { .each(function () {
$(this) $(this)
.find('td > span.div-horizontal') .find('td.v-giai > span ')
.each(function () { .each(function () {
numbers.push($(this).text()); numbers.push($(this).text());
}); });
// console.log(numbers); $(this)
$(this) .find('tr > td:first-child')
.find('tr > td:first-child') .each(function (i) {
.each(function (i) { const name = $(this).text();
const name = $(this).text(); console.log(name);
if (name !== 'Mã ĐB') { if (!name.includes('Mã ĐB')) {
// for (let i = 0; i < names.length; i++) { // for (let i = 0; i < names.length; i++) {
if (names.includes(name)) { if (names.includes(name)) {
return; return;
} else { } else {
names.push(name.split('.').join('_')); names.push(name.split('.').join('_'));
}
// }
} }
// } });
} });
}); numbers.splice(0, 1); // xóa phần tử đầu tiên
}); if (numbers.length > 0) {
if (numbers.length > 0) { for (let i = 0; i < names.length; i++) {
for (let i = 0; i < names.length; i++) { results[names[0]] = [numbers[0]];
objTimesNames[names[0]] = [numbers[0]]; results[names[1]] = [numbers[1]];
objTimesNames[names[1]] = [numbers[1]]; results[names[2]] = [numbers[2], numbers[3]];
objTimesNames[names[2]] = [numbers[2], numbers[3]]; results[names[3]] = [
objTimesNames[names[3]] = [ numbers[4],
numbers[4], numbers[5],
numbers[5], numbers[6],
numbers[6], numbers[7],
numbers[7], numbers[8],
numbers[8], numbers[9],
numbers[9], ];
]; results[names[4]] = [
objTimesNames[names[4]] = [ numbers[10],
numbers[10], numbers[11],
numbers[11], numbers[12],
numbers[12], numbers[13],
numbers[13], ];
]; results[names[5]] = [
objTimesNames[names[5]] = [ numbers[14],
numbers[14], numbers[15],
numbers[15], numbers[16],
numbers[16], numbers[17],
numbers[17], numbers[18],
numbers[18], numbers[19],
numbers[19], ];
]; results[names[6]] = [
objTimesNames[names[6]] = [ numbers[20],
numbers[20], numbers[21],
numbers[21], numbers[22],
numbers[22], ];
]; results[names[7]] = [
objTimesNames[names[7]] = [ numbers[23],
numbers[23], numbers[24],
numbers[24], numbers[25],
numbers[25], numbers[26],
numbers[26], ];
]; }
} }
}
// $('.list-link', html) // $('.list-link', html)
// .find('h2 > a:last-child') // .find('h2 > a:last-child')
// .each(function () { // .each(function () {
// const time = $(this).prop('innerHTML').split(' ')[1]; // const time = $(this).prop('innerHTML').split(' ')[1];
// times.push(time); // times.push(time);
// console.log(time); // console.log(time);
// }); // });
// if (times.length > 0) { // if (times.length > 0) {
// times.forEach(time => { // times.forEach(time => {
// for (let i = 0; i < names.length; i++) { // for (let i = 0; i < names.length; i++) {
// objTimesNames[time] = [...names]; // results[time] = [...names];
// } // }
// }); // });
// } // }
const nowDay = new Date(); const nowDay = new Date();
const calendar = const calendar =
nowDay.getDate() + nowDay.getDate() +
'/' + '/' +
(nowDay.getMonth() + 1 < 10 (nowDay.getMonth() + 1 < 10
? '0' + (nowDay.getMonth() + 1) ? '0' + (nowDay.getMonth() + 1)
: nowDay.getMonth() + 1) + : nowDay.getMonth() + 1) +
'/' + '/' +
nowDay.getFullYear(); nowDay.getFullYear();
// console.log(nowDay.getHours() + ':' + nowDay.getMinutes()); // console.log(nowDay.getHours() + ':' + nowDay.getMinutes());
// console.log(objTimesNames); // console.log(results);
const date = $('.class-title-list-link', html) const date = $('.class-title-list-link', html)
.first() .first()
.find('a:last-child') .find('a:last-child')
.text() .text()
.split(' ')[1]; .split(' ')[1];
res.status(200).json({ res.status(200).json({
countNumbers: numbers.length, countNumbers: numbers.length,
time: date, time: date,
objTimesNames, results,
});
})
.catch((err) => {
console.log(err);
return res.status(500).json({ msg: err });
}); });
});
} catch (e) { } catch (e) {
res.status(500).json({ msg: e }); res.status(500).json({ msg: e });
} }