Skip to content

Instantly share code, notes, and snippets.

@optikalefx
Created May 31, 2018 00:06
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 optikalefx/69e073049d4524c7101180c5bb72f143 to your computer and use it in GitHub Desktop.
Save optikalefx/69e073049d4524c7101180c5bb72f143 to your computer and use it in GitHub Desktop.
const puppeteer = require('puppeteer');
const fs = require('fs-extra');
const timeout = function (ms) {
return new Promise(resolve => setTimeout(resolve, ms));
};
const LoopFunction = async function (page) {
try {
const sections = await page.$$('#listaCursoSuperior .item-graduacao');
for (const section of sections) {
const paginaAtualSeleteor = await page.$('.paginacao li.active a');
const paginaAtualNome = await page.evaluate(paginaAtualSeleteor => paginaAtualSeleteor.innerText, paginaAtualSeleteor);
console.log(sections.length + '\n');
console.log(paginaAtualNome + '\n');
console.log('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%');
const seletorNome = await section.$('.campo.instituicao a');
const nomeFaculdade = await page.evaluate(seletorNome => seletorNome.innerText, seletorNome);
const seletorCurso = await section.$('.campo.curso a');
const nomeCurso = await page.evaluate(seletorCurso => seletorCurso.innerText, seletorCurso);
const seletorModalidade = await section.$('.campo.tags span:first-child');
const nomeModalidade = await page.evaluate(seletorModalidade => seletorModalidade.innerText, seletorModalidade);
const seletorTurno = await section.$('.campo.tags span:last-child');
const nomeTurno = await page.evaluate(seletorTurno => seletorTurno.innerText, seletorTurno);
const seletorLocal = await section.$('.campo.bairro span:last-child');
const nomeLocal = await page.evaluate(seletorLocal => seletorLocal.innerText, seletorLocal);
const seletorValorCheio = await section.$('.campo.valor span:first-child');
const nomeValorCheio = await page.evaluate(seletorValorCheio => seletorValorCheio.innerText.split(' ')[1], seletorValorCheio);
const seletorDescontoBolsa = await section.$('.campo.bolsa .destaque');
const nomeDescontoBolsa = await page.evaluate(seletorDescontoBolsa => seletorDescontoBolsa.innerText, seletorDescontoBolsa);
const seletorPreco = await section.$('.campo.valor .info.cor .destaque');
const nomePreco = await page.evaluate(seletorPreco => seletorPreco.innerText, seletorPreco);
const seletorPrecoCentavos = await section.$('.campo.valor .info.cor strong');
const nomePrecoCentavos = await page.evaluate(seletorPrecoCentavos => seletorPrecoCentavos.innerText, seletorPrecoCentavos);
//await fs.appendFile('educamais/teste.csv', `'${nomeFaculdade}','${nomeCurso}','${nomeModalidade}','${nomeTurno}','${nomeLocal}','${nomeValorCheio}','${nomeDescontoBolsa}','${nomePreco},'${nomePrecoCentavos}'\n`);
console.log('Faculdade: ', nomeFaculdade + '\nCurso: ', nomeCurso + '\nModalidade: ', nomeModalidade + '\nTurno: ', nomeTurno + '\nLocal: ', nomeLocal + '\nValor sem desconto: ', nomeValorCheio + '\nBolsa: ', nomeDescontoBolsa + '%' + '\nPreço: ', nomePreco + nomePrecoCentavos);
console.log('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%');
}
const nextBtnFIM = await page.$('.paginacao li.disabled:nth-last-child(2) a');
if (nextBtnFIM === null) {
await page.click('.paginacao li:nth-last-child(2) a');
await LoopFunction(page);
} else {
console.log('entrou no ELSE');
}
} catch (e) {
console.log('O erro foi do LOOP : ', e.stack);
}
}
const Loop1 = async function (page, indice) {
try {
const curosDisponiveis = await page.$$('#SuperiorPos .boxInstitucaoHome .lnkCursosDisponiveis span');
const setaDireita = await page.$('.carrosselBoxes-helpers .seta.seta-dir');
for (let k = 0; k < curosDisponiveis.length; k++) {
if (indice == 5) {
await setaDireita.click();
indice = 1;
}
await curosDisponiveis[k].click();
console.log(curosDisponiveis.length + ' cursos disponiveis\n');
console.log(indice + ' indice\n');
await LoopFunction(page);
indice++;
await page.goto('https://www.educamaisbrasil.com.br/', { waitUntil: 'networkidle0'});
await page.$$('#SuperiorPos .boxInstitucaoHome .lnkCursosDisponiveis span');
await Loop1(page, indice);
}
} catch (e) {
console.log('O erro foi no LOOP1', e);
}
};
(async function main() {
try {
console.log('iniciando conexão...');
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
page.setViewport({ width: 1280, height: 720 });
//page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36');
page.setDefaultNavigationTimeout(60000);
await page.setCacheEnabled(false);
console.log('acessando...');
await page.goto('https://www.educamaisbrasil.com.br/', { waitUntil: 'networkidle0'} );
await page.waitForSelector('span.ui-select-match-text.pull-left');
await page.click('span.ui-select-match-text.pull-left');
const inputCidade = '#listaInstituicoes input';
await page.waitForSelector(inputCidade);
await page.type(inputCidade, 'jacarei ');
await page.waitForSelector('.ui-select-choices-row.active');
await page.click('.ui-select-choices-row.active');
var indice = 1;
//await timeout(5000);
//const cidadeEscolhida = await page.$('#sel_cidade div span span:last-child');
//const cidadeEscolhidaNome = await page.evaluate(cidadeEscolhida => cidadeEscolhida.innerText, cidadeEscolhida);
//await fs.writeFile('educamais/'+cidadeEscolhidaNome+'.csv', 'Faculdade,Curso,Tipo,Turno,Modalidade,Local,Preco,Desconto em %,Link,Valor cheio,\n');
await Loop1(page, indice);
console.log('\nTerminou :)!!!');
await page.screenshot({ path: 'screenshot.png' });
//await browser.close();
} catch (e) {
console.log('O erro foi : ', e.stack);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment