Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Stanislas-Poisson/de3dafbba694735f8cfcae45ff5222e6 to your computer and use it in GitHub Desktop.
Save Stanislas-Poisson/de3dafbba694735f8cfcae45ff5222e6 to your computer and use it in GitHub Desktop.
Récupération de la liste des lycées

La liste officiel est disponible sur cette page le site education.gouv.fr

Ouvrir l'inspecteur, se mettre en console et copier-coller le code suivant :

function sleep(milliseconds) {
    var start = new Date().getTime();
    for (var i = 0; i < 1e7; i++) {
        if ((new Date().getTime() - start) > milliseconds) {
            break;
        }
    }
}

var schools = [];

$('.annuaire-recherche-resultats .annuaire-resultats-liste-etablissement > div .annuaire-etablissement-label a').each(function(i,el){
    school = {};
    school.name = el.textContent;
    school.href = el.href;
    schools.push(school);
});

console.log(schools.length);
$.each(schools, function (i, el) {
    console.log(i);
    $.ajax({
        url: el.href,
        async: false,
        success: function (data) {
            $data = $(data);

            el.type = $data.find('.fiche-type-etab').html().replace(/\n/g, ' ').trim();

            $type = $data.find('.annuaire-type-etab').html().replace('Établissement ', '').trim();
            $type = $type.substr(0, 1).toUpperCase()+$type.substr(1)
            el.secteur = $type;

            $info = $data.find('.annuaire-etablissement-infos-part3').html();

            $add = $info.match(/\n(.*)<br>\n/i);
            $add = $add[1].split('<br>');
            addTotal = $add.length;
            el.address = {};

            $.each($add, function(j, x) {
                if (j != (addTotal - 1) ) {
                    el.address['line_'+(j+1)] = x;
                } else {
                    x = x.match(/([\d]+) (.*)/i);
                    el.address.zipCode = x[1];
                    el.address.city = x[2];
                }
            });

            $tel = $info.match(/Tél. ([\d\s]+)/i);
            el.phone = $tel[1].replace(/\s/g, '').trim();

            el.uri = $data.find('.lien-site-etab').attr('href');

            delete el['href'];
        }
    });
    sleep(1000);
});

JSON.stringify(schools);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment