function requestHanlder(error, response, body) {


    // Tables to iterate on, built explicitly to comment with their content
    var interests = ["dati2",   // Anagrafica
                     "dati3",   // Origine sociale
                     "dati4",   // Studi secondari superiori
                     "dati5",   // Riuscita negli studi universitari
                     "dati6",   // Condizioni di studio
                     "dati7",   // Lavoro durante gli studi
                     "dati8",   // Giudizi sull'esperienza universitaria
                     "dati9",   // Conoscenze linguistiche ed informatiche
                     "dati10",  // Prospettive di studio
                     "dati11"]; // Prospettive di lavoro
                     
                     
    // TODO Check response == 200
    if(error) {
        console.log(error);
    }
    else {

        var $ = cheerio.load(body);
        let parsedTable = {}
        
        interests.forEach((table) => {

            parsedTable[table] = {};

            const rows = $('#'+table).find("tr");
            
            // Start explore fields of table
            for(let i=0; i<rows.length; i++) {

                const key = $(rows[i]).children("th").text();
                const value = $(rows[i]).children("td").text();
                const keyClass = rows[i].attribs.class;
                
                if(keyClass &&                          // keyClass != undefined
                    keyClass.indexOf("gruppo") != -1 && // keyClass contains substring "gruppo"
                    i < rows.length-1) {                // if last row, skip this check

                    // Class "gruppo*" should may children, check with hasSubList
                    parsedTable[table][key] = {};
                    let hasSublist = false;
                    let subKey = $(rows[i+1]).children("th").text();
                    let subValue = $(rows[i+1]).children("td").text();    
                    let subKeyClass = rows[i+1].attribs.class;

                    while(subKeyClass && subKeyClass.indexOf("gruppo") == -1) {

                        hasSublist = true;
                        parsedTable[table][key][subKey] = subValue;
                        i++;

                        if(i >= rows.length) break;

                        subKey = $(rows[i]).children("th").text();
                        subValue = $(rows[i]).children("td").text();
                        subKeyClass = rows[i].attribs.class;
                    }

                    if(hasSublist) i--; // If this gruppo has children step back (we are 1 step further)
                    else parsedTable[table][key] = value; // otherwise, set value to key

                }
                else if(key != "" && value != "") {
                    // Standalone table row
                    parsedTable[table][key] = value;
                }
            }
        });

        console.log(parsedTable);
        const json = JSON.stringify(parsedTable, null, 2);
        //fs.writeFileSync("./table.json", json, "utf8");
        // Invece invia a chiamante
    }
}
/**
 * Handler function for 'request'
 */
Handler function for 'request'
Handler function for ' request '
function requestHanlder(error, response, body) {
    var interests = ["dati2",   
                     "dati3",   
                     "dati4",   
                     "dati5",   
                     "dati6",   
                     "dati7",   
                     "dati8",   
                     "dati9",   
                     "dati10",  
                     "dati11"]; 
    if(error) {
        console.log(error);
    }
    else {
        var $ = cheerio.load(body);
        let parsedTable = {}
        interests.forEach((table) => {
            parsedTable[table] = {};
            const rows = $('#'+table).find("tr");
            for(let i=0; i<rows.length; i++) {
                const key = $(rows[i]).children("th").text();
                const value = $(rows[i]).children("td").text();
                const keyClass = rows[i].attribs.class;
                if(keyClass &&                          
                    keyClass.indexOf("gruppo") != -1 && 
                    i < rows.length-1) {                
                    parsedTable[table][key] = {};
                    let hasSublist = false;
                    let subKey = $(rows[i+1]).children("th").text();
                    let subValue = $(rows[i+1]).children("td").text();    
                    let subKeyClass = rows[i+1].attribs.class;
                    while(subKeyClass && subKeyClass.indexOf("gruppo") == -1) {
                        hasSublist = true;
                        parsedTable[table][key][subKey] = subValue;
                        i++;
                        if(i >= rows.length) break;
                        subKey = $(rows[i]).children("th").text();
                        subValue = $(rows[i]).children("td").text();
                        subKeyClass = rows[i].attribs.class;
                    }
                    if(hasSublist) i--; 
                    else parsedTable[table][key] = value; 
                }
                else if(key != "" && value != "") {
                    parsedTable[table][key] = value;
                }
            }
        });
        console.log(parsedTable);
        const json = JSON.stringify(parsedTable, null, 2);
    }
}
function requestHanlder ( error , response , body ) { var interests = [ "dati2" , "dati3" , "dati4" , "dati5" , "dati6" , "dati7" , "dati8" , "dati9" , "dati10" , "dati11" ] ; if ( error ) { console . log ( error ) ; } else { var $ = cheerio . load ( body ) ; let parsedTable = { } interests . forEach ( ( table ) => { parsedTable [ table ] = { } ; const rows = $ ( '#' + table ) . find ( "tr" ) ; for ( let i = 0 ; i < rows . length ; i ++ ) { const key = $ ( rows [ i ] ) . children ( "th" ) . text ( ) ; const value = $ ( rows [ i ] ) . children ( "td" ) . text ( ) ; const keyClass = rows [ i ] . attribs . class ; if ( keyClass && keyClass . indexOf ( "gruppo" ) != - 1 && i < rows . length - 1 ) { parsedTable [ table ] [ key ] = { } ; let hasSublist = false ; let subKey = $ ( rows [ i + 1 ] ) . children ( "th" ) . text ( ) ; let subValue = $ ( rows [ i + 1 ] ) . children ( "td" ) . text ( ) ; let subKeyClass = rows [ i + 1 ] . attribs . class ; while ( subKeyClass && subKeyClass . indexOf ( "gruppo" ) == - 1 ) { hasSublist = true ; parsedTable [ table ] [ key ] [ subKey ] = subValue ; i ++ ; if ( i >= rows . length ) break ; subKey = $ ( rows [ i ] ) . children ( "th" ) . text ( ) ; subValue = $ ( rows [ i ] ) . children ( "td" ) . text ( ) ; subKeyClass = rows [ i ] . attribs . class ; } if ( hasSublist ) i -- ; else parsedTable [ table ] [ key ] = value ; } else if ( key != "" && value != "" ) { parsedTable [ table ] [ key ] = value ; } } } ) ; console . log ( parsedTable ) ; const json = JSON . stringify ( parsedTable , null , 2 ) ; } }
Handler function for 'request'
Handler function for ' request '
// Tables to iterate on, built explicitly to comment with their content // Anagrafica // Origine sociale // Studi secondari superiori // Riuscita negli studi universitari // Condizioni di studio // Lavoro durante gli studi // Giudizi sull'esperienza universitaria // Conoscenze linguistiche ed informatiche // Prospettive di studio // Prospettive di lavoro // TODO Check response == 200 // Start explore fields of table // keyClass != undefined // keyClass contains substring "gruppo" // if last row, skip this check // Class "gruppo*" should may children, check with hasSubList // If this gruppo has children step back (we are 1 step further) // otherwise, set value to key // Standalone table row //fs.writeFileSync("./table.json", json, "utf8"); // Invece invia a chiamante