Skip to content

Instantly share code, notes, and snippets.

@jenhacool
Created March 10, 2021 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jenhacool/ecf69529293f5f20219cec94b2a6c2e5 to your computer and use it in GitHub Desktop.
Save jenhacool/ecf69529293f5f20219cec94b2a6c2e5 to your computer and use it in GitHub Desktop.
Dichvuthongtin
const puppeteer = require('puppeteer');
const fs = require('fs');
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
var listMst = fs.readFileSync('list.txt').toString().split('\n');
const Queue = require('bull');
const getDataQueue = new Queue('get data');
getDataQueue.process(async function(job, done) {
try {
const listMst = job.data.list;
const csvWriter = createCsvWriter({
path: `data-${job.data.number}.csv`,
header: [
{id: 'Mã số thuế', title: 'Mã số thuế'},
{id: 'Loại Giấy chứng thực cá nhân', title: 'Loại Giấy chứng thực cá nhân'},
{id: 'Số Giấy chứng thực cá nhân', title: 'Số Giấy chứng thực cá nhân'},
{id: 'Họ và tên', title: 'Họ và tên'},
{id: 'Giới tính', title: 'Giới tính'},
{id: 'Ngày sinh', title: 'Ngày sinh'},
]
});;
const browser = await puppeteer.launch({headless: true, args: ['--no-sandbox']});
const page = await browser.newPage();
await page.goto('https://dichvuthongtin.dkkd.gov.vn/inf/default.aspx');
const [button] = await page.$x("//a[contains(., 'Vai trò của cá nhân')]");
if (button) {
await button.click();
}
await page.waitForNavigation({waitUntil: 'load'});
await page.waitForSelector('input[name="ctl00$C$UC_PERS_LIST1$ENTERPRISE_CODEFilterFld"]');
for (var i = 0; i < listMst.length; i++) {
try {
await page.$eval('input[name="ctl00$C$UC_PERS_LIST1$ENTERPRISE_CODEFilterFld"]', (el, value) => el.value = value, listMst[i]);
await page.click('input[name="ctl00$C$UC_PERS_LIST1$BtnFilter"]');
await page.waitForSelector('table#ctl00_C_UC_PERS_LIST1_CtlList > tbody > tr + tr', {
timeout: 3000
});
let item = await page.evaluate((mst) => {
let persons = [];
$('table#ctl00_C_UC_PERS_LIST1_CtlList tbody tr').each(function(i) {
if(i == 0) {
return true
}
persons.push({
'Mã số thuế': mst,
'Loại Giấy chứng thực cá nhân': $(this).find('td:nth-child(2) a').text(),
'Số Giấy chứng thực cá nhân': $(this).find('td:nth-child(3) a').text(),
'Họ và tên': $(this).find('td:nth-child(4) a').text(),
'Giới tính': $(this).find('td:nth-child(5) a').text(),
'Ngày sinh': $(this).find('td:nth-child(6) a').text(),
})
});
$('table#ctl00_C_UC_PERS_LIST1_CtlList tbody').empty();
return persons;
}, listMst[i]);
csvWriter.writeRecords(item).then(() => {
console.log(`...Done ${i}`);
});
} catch(error) {
fs.appendFileSync('error.txt', listMst[i] + "\n")
}
}
await browser.close();
} catch(error) {
}
done();
});
getDataQueue.on('completed', (job, result) => {
console.log('completed client', job.data.number, result);
});
getDataQueue.on('error', (err) => {
console.log('error client', err);
});
getDataQueue.on('failed', function(job, err){
console.log('failed', job.data.number);
});
let number = 1;
while (listMst.length > 0) {
console.log('created', number);
getDataQueue.add(
{
list: listMst.splice(0, 300),
number
},
{
delay: 3000,
attempts: 5, // If job fails it will retry till 5 times
backoff: 5000 // static 5 sec delay between retry
}
)
number += 1;
}
const puppeteer = require('puppeteer');
const fs = require('fs');
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
var listMst = fs.readFileSync('list_1.txt').toString().split('\n');
const Queue = require('bull');
const getDataQueue = new Queue('get data');
getDataQueue.process(async function(job, done) {
const listMst = job.data.list;
const csvWriter = createCsvWriter({
path: `data-${job.data.number}.csv`,
header: [
{id: 'Mã số thuế', title: 'Mã số thuế'},
{id: 'Loại Giấy chứng thực cá nhân', title: 'Loại Giấy chứng thực cá nhân'},
{id: 'Số Giấy chứng thực cá nhân', title: 'Số Giấy chứng thực cá nhân'},
{id: 'Họ và tên', title: 'Họ và tên'},
{id: 'Giới tính', title: 'Giới tính'},
{id: 'Ngày sinh', title: 'Ngày sinh'},
]
});;
const browser = await puppeteer.launch({headless: true, args: ['--no-sandbox']});
const page = await browser.newPage();
await page.goto('https://dichvuthongtin.dkkd.gov.vn/inf/default.aspx');
const [button] = await page.$x("//a[contains(., 'Vai trò của cá nhân')]");
if (button) {
await button.click();
}
await page.waitForNavigation({waitUntil: 'load'});
await page.waitFor('input[name="ctl00$C$UC_PERS_LIST1$ENTERPRISE_CODEFilterFld"]');
for (var i = 0; i < listMst.length; i++) {
await page.$eval('input[name="ctl00$C$UC_PERS_LIST1$ENTERPRISE_CODEFilterFld"]', (el, value) => el.value = value, listMst[i]);
await page.click('input[name="ctl00$C$UC_PERS_LIST1$BtnFilter"]');
try {
await page.waitForSelector('table#ctl00_C_UC_PERS_LIST1_CtlList > tbody > tr + tr', {
timeout: 3000
});
let item = await page.evaluate((mst) => {
let persons = [];
$('table#ctl00_C_UC_PERS_LIST1_CtlList tbody tr').each(function(i) {
if(i == 0) {
return true
}
persons.push({
'Mã số thuế': mst,
'Loại Giấy chứng thực cá nhân': $(this).find('td:nth-child(2) a').text(),
'Số Giấy chứng thực cá nhân': $(this).find('td:nth-child(3) a').text(),
'Họ và tên': $(this).find('td:nth-child(4) a').text(),
'Giới tính': $(this).find('td:nth-child(5) a').text(),
'Ngày sinh': $(this).find('td:nth-child(6) a').text(),
})
});
$('table#ctl00_C_UC_PERS_LIST1_CtlList tbody').empty();
return persons;
}, listMst[i]);
csvWriter.writeRecords(item).then(() => {
console.log(`...Done ${i}`);
});
} catch(error) {
console.log(error)
}
}
await browser.close();
done();
});
let number = 1;
while (listMst.length > 0) {
getDataQueue.add({list: listMst.splice(0, 10), number})
number += 1;
}
const puppeteer = require('puppeteer');
const fs = require('fs');
var listMst = fs.readFileSync('list_2.txt').toString().split('\n');
listMst = listMst.splice(0, 100);
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const csvWriter = createCsvWriter({
path: 'data-2.csv',
header: [
{id: 'Mã số thuế', title: 'Mã số thuế'},
{id: 'Loại Giấy chứng thực cá nhân', title: 'Loại Giấy chứng thực cá nhân'},
{id: 'Số Giấy chứng thực cá nhân', title: 'Số Giấy chứng thực cá nhân'},
{id: 'Họ và tên', title: 'Họ và tên'},
{id: 'Giới tính', title: 'Giới tính'},
{id: 'Ngày sinh', title: 'Ngày sinh'},
]
});
(async () => {
const startTime = Date.now();
const browser = await puppeteer.launch({headless: true, args: ['--no-sandbox']});
const page = await browser.newPage();
await page.goto('https://dichvuthongtin.dkkd.gov.vn/inf/default.aspx');
const [button] = await page.$x("//a[contains(., 'Vai trò của cá nhân')]");
if (button) {
await button.click();
}
await page.waitForNavigation({waitUntil: 'load'});
await page.waitFor('input[name="ctl00$C$UC_PERS_LIST1$ENTERPRISE_CODEFilterFld"]');
let items = [];
for (var i = 0; i < listMst.length; i++) {
await page.$eval('input[name="ctl00$C$UC_PERS_LIST1$ENTERPRISE_CODEFilterFld"]', (el, value) => el.value = value, listMst[i]);
await page.click('input[name="ctl00$C$UC_PERS_LIST1$BtnFilter"]');
try {
await page.waitForSelector('table#ctl00_C_UC_PERS_LIST1_CtlList > tbody > tr + tr', {
timeout: 3000
});
let item = await page.evaluate((mst) => {
let persons = [];
$('table#ctl00_C_UC_PERS_LIST1_CtlList tbody tr').each(function(i) {
if(i == 0) {
return true
}
persons.push({
'Mã số thuế': mst,
'Loại Giấy chứng thực cá nhân': $(this).find('td:nth-child(2) a').text(),
'Số Giấy chứng thực cá nhân': $(this).find('td:nth-child(3) a').text(),
'Họ và tên': $(this).find('td:nth-child(4) a').text(),
'Giới tính': $(this).find('td:nth-child(5) a').text(),
'Ngày sinh': $(this).find('td:nth-child(6) a').text(),
})
});
$('table#ctl00_C_UC_PERS_LIST1_CtlList tbody').empty();
return persons;
}, listMst[i]);
items = items.concat(item)
} catch(error) {
console.log('Mã số thuế sai')
}
}
csvWriter.writeRecords(items).then(() => {
console.log(`...Done ${i}`);
});
await browser.close();
console.log('time', (Date.now() - startTime) / 1000)
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment