How to create a Jsoup.select.Elements with duplicate data?

后端 未结 2 1592
猫巷女王i
猫巷女王i 2021-01-23 02:17

I have a web-page:



        
相关标签:
2条回答
  • 2021-01-23 02:54

    It seems you have hit a known issue from Jsoup (see luksch's answer for details). However, here is a workaround working on Jsoup 1.8.3.

    table#timetable > tbody > tr > td.time
    

    SAMPLE CODE

    String html = "<table id=\"timetable\" class=\"table gradient-table\">\n<tbody>\n    <tr>\n      <td  class=\"time\">\n      <div>10:30 12:05</div>\n      </td>\n      <td  class=\"time\">\n      <div>12:30 14:05</div>\n      </td>\n      <td  class=\"time\">\n      <div>12:30 14:05</div>\n      </td>\n      <td  class=\"time\">\n      <div>14:30 16:05</div>\n      </td>\n      <td  class=\"time\">\n      <div>16:30 18:05</div>\n      </td>\n    </tr>\n</tbody>\n</table>";
    
    Document doc = Jsoup.parse(html);
    
    for (Element elt : doc.select("table#timetable > tbody > tr > td.time")) {
        System.out.println(elt.text());
    }
    

    OUTPUT

    10:30 12:05
    12:30 14:05
    12:30 14:05
    14:30 16:05
    16:30 18:05
    

    Tested on Jsoup 1.8.3

    0 讨论(0)
  • 2021-01-23 03:10

    You came across a known bug in JSoup 1.8.2 & JSoup 1.8.3. See issue #614 and #664

    To avoid this you should downgrade to Jsoup version 1.8.1 if possible or make sure to not use the select method of the Elements (plural!) class. CSS selectors on single elements or the whole dolument seem to be not affected, so you can also go with the solution of @Stephan.

    0 讨论(0)
提交回复
热议问题