How to parse HTML table using jsoup?

前端 未结 3 2020
盖世英雄少女心
盖世英雄少女心 2020-12-02 15:18

I am trying to parse HTML using jsoup. This is my first time working with jsoup and I read some tutorial on it as well. Below is my HTML table which I am trying to parse -

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-02 16:02

    What I would do in your case is first create an Object of your machine with all apropriate attributes. Then using Jsoup I would extract data and create an ArrayList, and then use logic to get data from the Arraylist.

    I am skipping the Object creation (since it is not the issue here) and I will name the Object as Machine

    Then using Jsoup I would get the row data like this:

    ArrayList list = new ArrayList();
    Document doc = Jsoup.parse(url, 3000);
    for (Element table : doc.select("table")) { //this will work if your doc contains only one table element
      for (Element row : table.select("tr")) {
        Machine tmp = new Machine();
        Elements tds = row.select("td");
        tmp.setClusterName(tds.get(3).text());
        tmp.setIp(tds.get(4).text());
        tmp.setStatus(tds.get(7).text());
        //.... and so on for the rest of attributes
        list.add(tmp);
      }
    }
    

    Then use a loop to get the values you need from the list:

    for(Machine x:list){
      if(x.getStatus().equalsIgnoreCase("up")){
        //machine with UP status found
        System.out.println("The Machine with up status is:"+x.getHostName());
      }
    }
    

    That's all. Please also note that this code is not tested and may contain some syntactical errors as it is written directly on this editor and not in an IDE.

提交回复
热议问题