Problems finding correct table

102 Views Asked by At

i'm trying to scrape a site using Jaunt ( https://ravit.is.fi/hevoset/1 ) and I'm having problems finding the correct table element to parse this table (in red: https://i.stack.imgur.com/Fnaep.png )

From the html, I assumed the correct element would be < table border=\"0\" cellpadding=\"3\" cellspacing=\"1\"> but the table marked in green also uses the same element so what would be the way to "choose" the correct table? Been trying tons of things to no avail but as I am pretty new to java, html and coding in general, I'm most likely missing something obvious

Also, I tried putting the data from the other table to the xls table but everything went to same cell so what do you need to do so it would look like this: https://i.stack.imgur.com/sMkxs.png ?

Thank you in advance

public class JauntTesti{
        public static void main(String[] args){
            int sivu = 1; 


            while (true) {
                try{
                    UserAgent userAgent = new UserAgent();  




                    if (sivu <= 1) {
                    userAgent.visit("https://ravit.is.fi/hevoset/" + sivu); 

                    String title = userAgent.doc.findFirst("<title>").getChildText();  //hakee ekan löytyvän otsikon stringiin title
                    System.out.println("\n" + sivu);


                    Element body = userAgent.doc.findFirst("<body>");
                    Element strong = body.findEach("<strong>");
                    Element strong2 = userAgent.doc.findEach("<td>");


                   Element strong3 = strong2.getElement(0).getElement(0).getElement(1).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0).getElement(1);
                   Element strong4 = strong2.getElement(0).getElement(0).getElement(1).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0).getElement(1).getElement(1);
                   Element strong5 = strong2.getElement(0).getElement(0).getElement(1).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0).getElement(2).getElement(1);
                   Element strong6 = strong2.getElement(0).getElement(0).getElement(1).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0).getElement(3).getElement(1);
                   Element strong7 = strong2.getElement(0).getElement(0).getElement(1).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0).getElement(4).getElement(1);
                   Element strong8 = strong2.getElement(0).getElement(0).getElement(1).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0).getElement(5).getElement(1);

                   Element test1 = strong2.getElement(0).getElement(0).getElement(1).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0);
                   Element test2 = strong2.getElement(0).getElement(0).getElement(1).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0).getElement(1).getElement(0);
                   Element test3 = strong2.getElement(0).getElement(0).getElement(1).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0).getElement(2).getElement(0);
                   Element test4 = strong2.getElement(0).getElement(0).getElement(1).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0).getElement(3).getElement(0);
                   Element test5 = strong2.getElement(0).getElement(0).getElement(1).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0).getElement(4).getElement(0);
                   Element test6 = strong2.getElement(0).getElement(0).getElement(1).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0).getElement(0).getElement(5).getElement(0);


                   String nimi = strong3.innerText();
                   String laji = strong4.innerText();
                   String sukupuoli = strong5.innerText();
                   String ika = strong6.innerText();

                   String valmentaja = strong7.innerText();
                   String omistaja = strong8.innerText();


                    while (true) {
                        if (test4.innerHTML().equals("<strong>IK&Auml;:</strong> ")){
                            ika = strong6.innerText();
                            break;   
                        }
                        ika = " ";
                        break;
                    }
                    while (true) {
                        if (test4.innerHTML().equals("<strong>VALMENTAJA:</strong> ")){
                            valmentaja = strong6.innerText();
                            break;   
                        }
                        if (test5.innerHTML().equals("<strong>VALMENTAJA:</strong> ")){
                            valmentaja = strong7.innerText();
                            break;   
                        }
                        valmentaja = "-1";
                        break;
                    }
                    while (true) {
                        if (test4.innerHTML().equals("<strong>OMISTAJA:</strong> ")){
                            omistaja = strong6.innerText();
                            break;   
                        }
                        if (test5.innerHTML().equals("<strong>OMISTAJA:</strong> ")){
                            omistaja = strong7.innerText();
                            break;   
                        }
                        if (test6.innerHTML().equals("<strong>OMISTAJA:</strong> ")){
                            omistaja = strong8.innerText();
                            break;   
                        }
                        omistaja = "-1";
                        break;
                    }
                    Table taulukko2 = userAgent.doc.getTable("<table border=\"0\" cellpadding=\"3\" cellspacing=\"1\">");
                Elements taul1 = taulukko2.getCol(0);
                for(Element element : taul1) System.out.println(taul1.innerText());


                    ika = ika.replace(" v","");
                    //int ikav = Integer.parseInt(ika);
                    System.out.println("Nimi: " + nimi);
                    System.out.println("Laji: " + laji);
                    System.out.println("Sukupuoli: " + sukupuoli);
                    System.out.println("Ikä: " + ika);
                    System.out.println("Valmentaja: " + valmentaja);
                    System.out.println("Omistaja: " + omistaja);

                    try {
                        String filename = "C:/sheets/" + sivu + ".xls";
                        HSSFWorkbook workbook = new HSSFWorkbook();
                        HSSFSheet sheet = workbook.createSheet("FirstSheet");
                        sheet.setColumnWidth(0, 5000);
                        sheet.setColumnWidth(1, 5000);
                        sheet.setColumnWidth(2, 3000);
                        sheet.setColumnWidth(3, 2000);
                        sheet.setColumnWidth(4, 4000);
                        sheet.setColumnWidth(5, 8000);
                        HSSFRow rowhead = sheet.createRow((short)0);
                        rowhead.createCell(0).setCellValue("NIMI");
                        rowhead.createCell(1).setCellValue("LAJI");
                        rowhead.createCell(2).setCellValue("SUKUPUOLI");
                        rowhead.createCell(3).setCellValue("IKÄ");
                        rowhead.createCell(4).setCellValue("VALMENTAJA");
                        rowhead.createCell(5).setCellValue("OMISTAJA");
                        //rowhead.createCell(6).setCellValue(taul1.innerText());

                        HSSFRow row = sheet.createRow((short)1);
                        row.createCell(0).setCellValue(nimi);
                        row.createCell(1).setCellValue(laji);
                        row.createCell(2).setCellValue(sukupuoli);
                        row.createCell(3).setCellValue(ika);
                        row.createCell(4).setCellValue(valmentaja);
                        row.createCell(5).setCellValue(omistaja);

                        FileOutputStream fileOut = new FileOutputStream(filename);
                        workbook.write(fileOut);
                        fileOut.close();
                        workbook.close();

                   } catch ( Exception ex ) {
                   System.out.println(ex);
                   }


                    sivu++;
                    } else {
                    break;
                    }
                }

                catch(JauntException e){
                System.err.println(e);  
                }
            }
        }
    }
1

There are 1 best solutions below

0
On

With the univocity-html-parser, you can get all details from all tables. Not sure how you need to organize your data, but this should give you some guidance:

    HtmlEntityList entityList = new HtmlEntityList();

    HtmlEntitySettings person = entityList.configureEntity("person");
    addFields(person, "NIMI", "LAJI", "SUKUPUOLI", "IKÄ", "VALMENTAJA", "OMISTAJA");

    //not sure if you need the fields under "URAN TILASTOT", but here we go:
    addFields(person, "STARTIT", "VOITOT", "2. SIJAT", "3. SIJAT", "VOITTOSUMMA");

    //also not not sure if you need the fields under "Tilastot, kun kenkiä riisuttu pois", but here we go again:
    addExactFields(person, "ȻȻ, ȻC tai CȻ", "ȻȻ", "ȻC", "CȻ", "ENNÄTYSAJAT", "RYHMÄLÄHTÖ", "TASOITUSAJO");

    //I have no clue what the tables mean, so I'm calling them "table 1" and "table 2"
    HtmlEntitySettings table1 = entityList.configureEntity("table1");
    captureColumns(table1, "VUOSI", "STARTIT", "VOITOT", "2. SIJAT", "3. SIJAT", "VOITTOSUMMA", "RYHMÄ", "TASOITUS");

    HtmlEntitySettings table2 = entityList.configureEntity("table2");
    captureColumnsInLastTable(table2, "R", "PVM", "L", "R-NRO", "MATKA", "S", "KMA", "HYL", "KERR.", "PALK.", "OHJ.", "VALM.");

    HtmlParser parser = new HtmlParser(entityList);
    Results<HtmlParserResult> results = parser.parse(new UrlReaderProvider("https://ravit.is.fi/hevoset/1"));

    printResult(results.get("person"));
    printResult(results.get("table1"));
    printResult(results.get("table2"));

Which uses the following methods:

private void addFields(HtmlEntitySettings entity, String... labels) {
    for (String label : labels) {
        entity.addField(label).match("td")
                .withText(label)
                .not().classes("heppatilastohead")
                .matchNext("td").getText();
    }
}

private void addExactFields(HtmlEntitySettings entity, String... labels) {
    for (String label : labels) {
        entity.addField(label).match("td").withExactText(label).matchNext("td").getText();
    }
}

private void captureColumns(HtmlEntitySettings entity, String... headers) {
    for (String header : headers) {
        entity.addField(header)
                .match("td")
                .underHeader("td").withExactText(header)
                .getText();
    }
}

private void captureColumnsInLastTable(HtmlEntitySettings entity, String... headers) {
    for (String header : headers) {
        entity.addField(header)
                .match("form")
                .match("tr").not().at(2)
                .match("td")
                .underHeader("td").withExactText(header)
                .getText();
    }
}

private void printResult(HtmlParserResult result) {
    System.out.println("\nValues of [" + result.getEntityName() + "]");
    for (HtmlRecord record : result.iterateRecords()) {
        System.out.println(record.fillFieldMap(new LinkedHashMap<String, String>()));
    }
}

The output of this code is:

Values of [person]
{NIMI=Bernard Gazeau, LAJI=Lämminverinen, SUKUPUOLI=Ruuna, IKÄ=18 v, VALMENTAJA=Hannele Haapala, OMISTAJA=HaapalaHannele, Mouhijärvi, STARTIT=6, VOITOT=0, 2. SIJAT=0, 3. SIJAT=0, VOITTOSUMMA=680 €, ȻȻ, ȻC tai CȻ=0: 0-0-0, ȻȻ=0: 0-0-0, ȻC=0: 0-0-0, CȻ=0: 0-0-0, ENNÄTYSAJAT=null, RYHMÄLÄHTÖ=null, TASOITUSAJO=20,1 ke}

Values of [table1]
{VUOSI=2009, STARTIT=1, VOITOT=0, 2. SIJAT=0, 3. SIJAT=0, VOITTOSUMMA=140, RYHMÄ=null, TASOITUS=20,1 ke}
{VUOSI=2008, STARTIT=3, VOITOT=0, 2. SIJAT=0, 3. SIJAT=0, VOITTOSUMMA=420, RYHMÄ=null, TASOITUS=21,5 ke}
{VUOSI=2006, STARTIT=2, VOITOT=0, 2. SIJAT=0, 3. SIJAT=0, VOITTOSUMMA=120, RYHMÄ=null, TASOITUS=22,2 ke}
{VUOSI=YHT, STARTIT=6, VOITOT=0, 2. SIJAT=0, 3. SIJAT=0, VOITTOSUMMA=680, RYHMÄ=null, TASOITUS=20,1 ke}

Values of [table2]
{R=T, PVM=12.05.09, L=1, R-NRO=5, MATKA=2120, S=p, KMA=null, HYL=p, KERR.=0,0, PALK.=0, OHJ.=M Forss, VALM.=Haapala}
{R=TK, PVM=10.04.09, L=2, R-NRO=3, MATKA=2120, S=4, KMA=20,1, HYL=null, KERR.=25,6, PALK.=140, OHJ.=M Forss, VALM.=Haapala}
{R=TK, PVM=31.10.08, L=3, R-NRO=6, MATKA=2120, S=4, KMA=22,7, HYL=null, KERR.=104,2, PALK.=240, OHJ.=H Hell, VALM.=Haapala}
{R=T, PVM=04.03.08, L=2, R-NRO=10, MATKA=2100, S=8, KMA=21,5, HYL=null, KERR.=99,6, PALK.=100, OHJ.=H Hell, VALM.=Haapala}
{R=P, PVM=17.02.08, L=10, R-NRO=1, MATKA=2100, S=5, KMA=23,6, HYL=null, KERR.=96,1, PALK.=80, OHJ.=H Hell, VALM.=Haapala}
{R=T, PVM=15.01.08, L=KL1, R-NRO=3, MATKA=2120, S=kl, KMA=22,4, HYL=null, KERR.=0,0, PALK.=0, OHJ.=H Hell, VALM.=Haapala}
{R=T, PVM=18.12.07, L=KL1, R-NRO=1, MATKA=2120, S=kl hlo, KMA=25,2, HYL=hlo, KERR.=0,0, PALK.=0, OHJ.=H Hell, VALM.=Haapala}
{R=F, PVM=15.01.06, L=2, R-NRO=1, MATKA=2140, S=5, KMA=22,2, HYL=null, KERR.=21,4, PALK.=120, OHJ.=H Kamppuri, VALM.=Haapala}
{R=F, PVM=08.01.06, L=1, R-NRO=1, MATKA=2140, S=8, KMA=22,9, HYL=null, KERR.=4,0, PALK.=0, OHJ.=Ha Korpi, VALM.=Haapala}

Hope this can be useful to you.

Disclosure: I'm the author of this library. It's commercial closed source but it can save you a lot of development time.